1. Web. cofnig database connection
<Add name ="Moviedbcontext"
Connectionstring ="Data Source =.; initial catalog = movies; Integrated Security = true"
Providername ="System. Data. sqlclient"/>
Ii. initialize the database and data
UsingSystem. componentmodel. dataannotations;
Add the reference and the following initial class to movie. CS of the model class.
Public Class Movieinitializer: dropcreatedatabaseifmodelchanges <moviedbcontext>
{
Protected Override Void Seed (moviedbcontext context)
{
VaR Movies = New List <movie>
{
New Movie {Title = " When Harry Met Sally " ,
Price = 4.6 m },
New Movie {Title = " Ghost " ,
Price = 200.0 m },
};
Movies. foreach (D => context. Movies. Add (d ));
}
}
Add the following in protected void application_start () of global. asax. CS:Code:
Database. setinitializer <moviedbcontext> (NewMovieinitializer ());
3. Add moviescontrollers
Details note viewresult should be changed to: actionreuslt because we need to add an httpnotfound and it cannot return the viewresult object
PublicActionresult details (IntId =0)
{
Movie movie = dB. Movies. Find (ID );
If(MOVIE =Null)
{
ReturnHttpnotfound ();
}
ReturnView (movie );
}
It is best to give an initial value like edit and delte to prevent the submitted ID from being empty.
PublicActionresult edit (IntId =2)
{
Movie movie = dB. Movies. Find (ID );
If(MOVIE =Null)
{
ReturnHttpnotfound ();
}
ReturnView (movie );
}
Mvc3 improves the deletion function and adds deletion confirmation to prevent malicious code from deleting data without confirmation.
When you click Delete Connection, only the confirmation information is returned:
PublicActionresult Delete (IntID)
{
Movie movie = dB. Movies. Find (ID );
ReturnView (movie );
}
Click "OK" to submit and delete the post.
A) Check the Delete. cshtml file in the view to delete the source file:
@ Using (html. beginform ()){
<P>
<Input type ="Submit"Value ="Delete"/> |
@ Html. actionlink ("Back to list","Index")
</P>
B) The automatically generated action = "/movies/delete/1 is displayed in the confirmation deletion source file of HTML generation:
<Form action ="/Movies/delete/1"Method ="Post"> <P>
<Input type ="Submit"Value ="Delete"/> |
<A href ="/Movies"> Back to list </a>
</P>
</Form>
The following are the actions performed after deletion:
[Httppost, actionname ("Delete")]
PublicActionresult deleteconfirmed (IntId =0)
{
Movie movie = dB. Movies. Find (ID );
If(MOVIE =Null)
{
ReturnHttpnotfound ();
}
DB. Movies. Remove (movie );
DB. savechanges ();
ReturnRedirecttoaction ("Index");
}
Iv. Content Search and filtering
First, let's look at the searchindex. cshtml view page:
@ Using (html. beginform (" Searchindex " , " Movies " , Formmethod. Get ))
{
<P> genre: @ html. dropdownlist ( " Moviegenre " , " All " ) </P>
<P> title: @ html. Textbox ( " Searchstring " ) <Input type = " Submit " Value = " Filter " /> </P>
}
The following is the controller:
Public Actionresult searchindex ( String Moviegenre, String Searchstring)
{
VaR Genrelst = New List < String > ();
VaR Genreqry = From D In DB. Movies Orderby D. Genre Select D. Genre;
Genrelst. addrange (genreqry. Distinct ());
Viewbag. moviegenre = New Selectlist (genrelst );
VaR Movies =From M In DB. Movies Select M;
If (! String. isnullorempty (searchstring ))
{
Movies = movies. Where (S => S. Title. Contains (searchstring ));
}
If ( String . Isnullorempty (moviegenre ))
Return View (movies );
Else
{
Return View (movies. Where (x => X. Genre = moviegenre ));
}
}
The following is the generated HTML page.Source code:
<Form action = " /Movies/searchindex " Method = " Get " >
Genre:
< Select Id = " Moviegenre " Name = " Moviegenre " >
<Option value = "" > All </option>
<Option> comedy </option>
<Option> romanti comedy </option>
</ Select >
<P> title: <input id = " Searchstring " Name =" Searchstring " Type = " Text " Value = "" /> <Input type = " Submit " Value = " Filter " /> </P>
</Form>
5. moviedbcontext of the movie class and the database connection context class. Pay attention to reference using system. Data. entity;
using system. data. entity;
Public class movie
{< br> Public int ID { Get ; set ;}< br> Public string title { Get ; set ;}< br> Public decimal price { Get ; set ;}< BR >}< br> Public class moviedbcontext: dbcontext
{< br> Public dbset
movies {
Get ;
set ;}< BR >}
6. Validation of field rules
Public Int Id { Get ; Set ;}
[Required (errormessage = " Enter the title " )]
Public String Title { Get ; Set ;}
[Required (errormessage = " Price required " )]
[Range ( 1 , 100 , Errormessage = " Price must be between $1 and $100 " )]
[Displayformat (dataformatstring = " {0: c} " )]
Public Decimal Price { Get ; Set ;}