ASP. NET MVC uses remote attribute validation

Source: Internet
Author: User

Remoteattribute is a validation feature of ASP. In the SYSTEM.WEB.MVC namespace

Here's an example to illustrate

Many systems have a member of this function, members in the front desk registration, the user name can not be duplicated with the existing user name, but also required to enter the mobile phone number to register, while mobile phone number also need to verify whether duplicate, the following is the entity class

/// <summary>///member/// </summary> Public classmember{ Public intId {Get;Set; } [Required (ErrorMessage="please fill in the user name")] [Remote ("CheckName","Member", HttpMethod ="POST")]     Public stringName {Get;Set; } [Required (ErrorMessage="please fill in the password")] [Stringlength ( -, errormessage ="please fill in the 6 to 16 digit password", Minimumlength =6)]     Public stringPassword {Get;Set; } [Required (ErrorMessage="please fill in the mobile phone number")] [RegularExpression (@"^1\d{10}$", errormessage ="cell phone number format error")] [Remote ("Checkmobile","Member", HttpMethod ="POST")]     Public stringMobile {Get;Set; }}
View Code

Controller class

 Public classMembercontroller:controller {//Get:member         PublicActionResult Index () {returnView (); }         PublicActionResult Create () {returnView (); } [HttpPost] PublicActionResult Create (Member model) {if(modelstate.isvalid) {}returnView (model); } [HttpPost] PublicJsonresult CheckName (stringName) {            //This assumes that there is already a test member, if the registration is also filled in the test member, there is already a member, verification does not pass//This is just a simulation, the actual situation is to read the database and so on to determine whether there is a member of the user name            if(!string. Isnullorwhitespace (Name) && name.trim () = ="Test")            {                returnJson ("User name"+ Name +"already exists", Jsonrequestbehavior.allowget); }            returnJson (true, Jsonrequestbehavior.allowget); } [HttpPost] PublicJsonresult Checkmobile (stringMobile) {            //This assumes that you already have a mobile number 10000000000 already registered and if you register with that number, the verification does not pass//Note: Here the number 10000000000 does not have authenticity, just for example use//here is just a simulation, the actual situation is to read the database and so on to determine whether there is a member of the mobile phone number            if(!string. Isnullorwhitespace (Mobile) && mobile.trim () = ="10000000000")            {                returnJson ("mobile phone number has been registered", Jsonrequestbehavior.allowget); }            returnJson (true, Jsonrequestbehavior.allowget); }    }
View Code

View

@model remotedemoweb.models.member@{viewbag.title = "Create";    Html.enableclientvalidation (); Html.enableunobtrusivejavascript ();}<H2>Create</H2>@using (Html.BeginForm ()) {@Html. AntiForgeryToken ()<Divclass= "Form-horizontal">        <h4>Member</h4>        <HR/>@Html. ValidationSummary (True, "", new {@class = "Text-danger"})<Divclass= "Form-group">@Html. labelfor (model = model. Name, htmlattributes:new {@class = "Control-label col-md-2"})<Divclass= "Col-md-10">@Html. editorfor (model = model. Name, new {htmlattributes = new {@class = "Form-control"}}) @Html. validationmessagefor (model = mode L.name, "", new {@class = "Text-danger"})</Div>        </Div>        <Divclass= "Form-group">@Html. labelfor (model = model. Password, htmlattributes:new {@class = "Control-label col-md-2"})<Divclass= "Col-md-10">@Html. editorfor (model = model. Password, new {htmlattributes = new {@class = "Form-control"}}) @Html. validationmessagefor (model = Model. Password, "", new {@class = "Text-danger"})</Div>        </Div>        <Divclass= "Form-group">@Html. labelfor (model = model. Mobile, htmlattributes:new {@class = "Control-label col-md-2"})<Divclass= "Col-md-10">@Html. editorfor (model = model. Mobile, new {htmlattributes = new {@class = "Form-control"}}) @Html. validationmessagefor (model = mo Del. Mobile, "", new {@class = "Text-danger"})</Div>        </Div>        <Divclass= "Form-group">            <Divclass= "Col-md-offset-2 col-md-10">                <inputtype= "Submit"value= "Create"class= "Btn Btn-default" />            </Div>        </Div>    </Div>}<Div>@Html. ActionLink ("Back to List", "Index")</Div>@section scripts{<Scriptsrc= "~/scripts/jquery.validate.js"></Script><Scriptsrc= "~/scripts/jquery.validate.unobtrusive.js"></Script>    }
View Code

This calls the Html.enableclientvalidation ();   and Html.enableunobtrusivejavascript (); Simultaneous introduction of Jquery.validate.js and Jquery.validate.unobtrusive.js

Front desk view and fill in information

When you fill in the existing user name and mobile phone number

The above example is based on ASP. NET MVC 5

ASP. NET MVC uses remote attribute validation

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.