ASP. net mvc learning 7. Add validation attributes for the fields of Model Class (validation attribuate)

Source: Internet
Author: User

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.IsValidComeCreate

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.DateRegularExpression

As a result, data sources are created to support 'moviedbcontext'The content model has changed




Detail Template Delete Template

See You...



Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.