Reference: http://www.asp.net/mvc/tutorials/mvc-4/getting-started-with-aspnet-mvc4/adding-validation-to-the-model
Content:
], [], [] Update Model Class
Movie
{
ID {;;}
[Required]
Title {;;}
[DisplayFormat (DataFormatString =)]
DateTime ReleaseDate {;;}
[Required]
Genre {;;}
[Range (,)]
[DisplayFormat (DataFormatString =)]
Price {;;}
[StringLength ()]
Rating {;;}
} View Code
Run the program:
Databasetable
1,
ActionResult Index ()
{
MovieDBContext db = MovieDBContext ();
Movie movie = Movie ();
Movie. Title =;
Movie. Price =;
Db. Movies. Add (movie );
Db. SaveChanges ();
View (db. Movies. ToList ());
} View Code
II,
We add a new
Movie class [Require] validation attribute
III,
ModelState.IsValid
ComeCreate
ActionResult Create (Movie movie)
{
(ModelState. IsValid)
{
Db. Movies. Add (movie );
Db. SaveChanges ();
RedirectToAction ();
}
View (movie );
} View Code
2, ActionResult Create
ModelState. IsValid = false
Equivalent
IV,
A set of built-in view template models are also provided.
DataType.Date
RegularExpression
As a result, data sources are created to support 'moviedbcontext'The content model has changed
Detail Template Delete Template
See You...