Record learning MVC process, MVC validation (iv)

Source: Internet
Author: User

1.MVC Self-band check

Create a new User entity class
public class Users {public int Id {get; set;} [Stringlength (5,errormessage = "* Length must be <5")] [Required (errormessage = "* Name required")] public string UserName {get; set;} [RegularExpression (@ "^\d+$")] [Range (18,100)] [Required (errormessage = "*")] public int age{get; set;} }

New controller users and create mandatory type views with the creation template

   Public ActionResult Index ()        {            return View ();        }        Public ActionResult Add ()        {            return View ();        }        [HttpPost]        Public ActionResult Add (user user)        {            if (modelstate.isvalid)            {                //modelstate.isvalid = True checksum successful            }            Return redirecttoaction ("Index");        }

  

Front Page view requires these 3 JS support

<script src= "~/scripts/jquery-1.8.2.min.js" ></script>    <script src= "~/scripts/ Jquery.validate.min.js "></script>    <script src=" ~/scripts/jquery.validate.unobtrusive.min.js " ></script>

2. Global client (server side) check on or off

In the Web. config configuration

<appSettings>    <add key= "webpages:enabled" value= "false"/> <add    key= "enableclientvalidation "Value=" true "/>//global client check on    <add key=" Enableunobtrusivejavascript "value=" true "/>//global service-side checksum open  </appSettings>

On a page.

@{        html.enableclientvalidation (FALSE);//Current page client validation off        Html.enableunobtrusivejavascript (false);// Current server-side validation off    }

Record learning MVC process, MVC validation (iv)

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.