MVC3----Data annotations and Validation (2) detailed remote verification and compare verification

Source: Internet
Author: User
Tags actionlink

Remote verification

Profile:

If you want to implement the same as user registration, do not allow duplicate accounts, you can use Remote authentication. The remote feature allows client-side validation logic to be performed using the server's callback function. It just enters the character in the text box to submit a GET request to the server, remote authentication only supports input time verification, does not support the validation when committed, there is a certain security risk. So we have to verify at the time of submission that the validation failed and add the Modelerror

650) this.width=650; "title=" Sogou 20150109162845.png "alt=" wkiol1svku7btehaaagarmv3gzs466.jpg "src="/http S3.51cto.com/wyfs02/m01/58/6b/wkiol1svku7btehaaagarmv3gzs466.jpg "/>

Realize:

-------Model Code:

Using system;using system.collections.generic;using system.linq;using system.web;using  System.Data.Entity;using System.ComponentModel;using System.ComponentModel.DataAnnotations; using system.web.mvc;//the required namespace namespace schoolmanagedomw.models{    public  class schooltype    {        [key]         public virtual int st_id { get; set;  }        //        the method to invoke              Controller name          [remote ("Checkusername",  "Schooltype")]//remote validation          public virtual string st_name{get;set;}                 public virtual list<school> schools { get; set; }     }}

-------Controller code:

The name control you need:

Using System.Web.Security;
Using System.Web.UI;

       private SchoolDBContext db = new  Schooldbcontext ();        /// <summary>         ///  define a method to make a unique judgment         ///  </summary>        /// <param name= "St_name" >< /param>        /// <returns></returns>         private bool isdistinctstname (String st_name)          {             if  (db. Schooltypes.where (R => r.st_name == st_name). ToList (). count > 0)                  return true;            else                 return false;        }                  ///  <summary>        ///  called methods          /// </summary>        /// < Param name= "St_name" ></param>        /// < Returns></returns>        [outputcache (Location =  Outputcachelocation.none, nostore = true)]//plus Clear Cache          public jsonresult checkusername (String st_name)          {            if  (Isdistinctstname (st_name))              {                 return json ("User name is not unique",  jsonrequestbehavior.allowget);             }             else            {                 return json ( True, jsonrequestbehavior.allowget);             }        }                         [HttpPost]      &nbSp;  public actionresult create (Schooltype schooltype)          {            //submit to server to make a judgment              if  (Isdistinctstname (schooltype.st_name ))             {                 modelstate.addmodelerror ("St_name",  " The user name is not unique ");            }             if  (modelstate.isvalid)              {                 db. Schooltypes.add (schooltype);             &Nbsp;   db. SaveChanges ();                 Return redirecttoaction ("Index");               }            return view ( Schooltype);         }

 -----View Code:

@model  SchoolManageDomw.Models.SchoolType@{    ViewBag.Title =  "Create";} 

Compare verification

Profile:

If you need to compare validation, such as whether the password is consistent, etc., you can verify with compare

650) this.width=650; "title=" Sogou 20150109165515.png "alt=" wkiol1svl4lz_7enaabvfuny5ns483.jpg "src="/http S3.51cto.com/wyfs02/m02/58/6c/wkiol1svl4lz_7enaabvfuny5ns483.jpg "/>

Realize:

-----Model Code:

Using system;using system.collections.generic;using system.linq;using system.web;using  System.Data.Entity;using System.ComponentModel;using System.ComponentModel.DataAnnotations; Using system.web.mvc;namespace schoolmanagedomw.models{    public class  SchoolType    {        [Key]         public virtual int st_id { get; set; }         [required]  //not allowed to empty          public virtual string st_name{get;set;}         [compare ("St_name")]         public virtual string st_nameConfirm { get; set; }         public virtual list<school> schools { get; set; }    }} 

-----View Code:

@model  SchoolManageDomw.Models.SchoolType@{    ViewBag.Title =  "Create";} 

This article is from the "program Ape's Home--hunter" blog, please be sure to keep this source http://962410314.blog.51cto.com/7563109/1601276

MVC3----Data annotations and Validation (2) detailed remote verification and compare verification

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.