ASP. net mvc implements Remote Property Verification using the Remote feature, mvcremote

Source: Internet
Author: User

ASP. net mvc implements Remote Property Verification using the Remote feature, mvcremote

RemoteAttribute is a validation feature of asp.net mvc, which is located in the namespace of System. Web. Mvc

The following example is used to describe

This function is available in many systems. When a member registers at the front-end, the user name cannot be the same as the existing user name. The user also needs to enter a mobile phone number to register and verify whether the mobile phone number is repeated, below are Entity classes

/// <Summary> /// Member /// </summary> public class Member {public int Id {get; set ;} [Required (ErrorMessage = "Enter the user Name")] [Remote ("CheckName", "Member", HttpMethod = "POST")] public string Name {get; set ;} [Required (ErrorMessage = "Enter Password")] [StringLength (16, ErrorMessage = "enter a 6-to 16-bit password", MinimumLength = 6)] public string Password {get; set;} [Required (ErrorMessage = "Please enter your mobile phone number")] [RegularExpression (@ "^ 1 \ d {10} $ ", errorMessage = "Incorrect Mobile Phone Number Format")] [Remote ("CheckMobile", "Member", HttpMethod = "POST")] public string Mobile {get; set ;}}
View Code

Controller class

Public class MemberController: Controller {// GET: Member public ActionResult Index () {return View ();} public ActionResult Create () {return View ();} [HttpPost] public ActionResult Create (Member model) {if (ModelState. isValid) {} return View (model);} [HttpPost] public JsonResult CheckName (string Name) {// assume that you already have the member test, if the member test is entered during registration, the member already exists. The verification fails. // This is just a simulation. The actual situation is to read the database and determine whether the user name exists. F (! String. isNullOrWhiteSpace (Name) & Name. trim () = "test") {return Json ("username" + Name + "already exists", JsonRequestBehavior. allowGet);} return Json (true, JsonRequestBehavior. allowGet);} [HttpPost] public JsonResult CheckMobile (string Mobile) {// assume that the Mobile phone number 10000000000 has been registered. If you use this number to register, verification fails. // note: The number 10000000000 here is not authentic, but it is used as an example. // It is only 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") {return Json ("the mobile phone number has been registered", JsonRequestBehavior. allowGet);} return Json (true, JsonRequestBehavior. allowGet );}}
View Code

View

@model RemoteDemoWeb.Models.Member@{    ViewBag.Title = "Create";    Html.EnableClientValidation();    Html.EnableUnobtrusiveJavaScript();}View Code

Here, Html. EnableClientValidation (); and Html. EnableUnobtrusiveJavaScript () are called. jquery. validate. js and jquery. validate. unobtrusive. js are introduced.

Browser and enter information

 

When you enter an existing user name and mobile phone number

The above example is based on ASP. net mvc 5

 

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.