Asp.net MVC learning diary 14 (verification submitted on the page)

Source: Internet
Author: User

First, let's see how MVC works.

1. Create a person class

Public class person
{
[Displayname ("first name"), stringlength (10)]
Public String firstname {Get; set ;}
[Displayname ("middle name"), stringlength (50)]
Public String middlename {Get; set ;}
[Displayname ("last name"), stringlength (50), required]
Public String lastname {Get; set ;}
[Displayname ("Birth date"), datatype (datatype. Date)]
Public datetime birthdate {Get; set ;}
[Datatype (datatype. emailaddress), required]
Public String email {Get; set ;}
Public String phone {Get; set ;}
Public String postcode {Get; set ;}
[Datatype (datatype. multilinetext)]
Public String notes {Get; set ;}
}

2. homecontroller

Public actionresult index ()
{
Viewdata ["message"] = "Welcome to ASP. net mvc! ";

Return view (new person ());
}

[Httppost]
Public actionresult index (formcollection form)
{
VaR person = new person ();
Tryupdatemodel (person );
Return view (person );
}

3. Index. aspx

<% Using (html. beginform () {%>
<%: HTML. editorformodel () %>
<Input type = "Submit" name = "Submit" value = "Submit"/>
<% }%>

Let's see how jquery works.

1. Go to http://code.google.com/p/arscloud/source/browse/trunk/MvcFutures/MvcFutures? R = 7 download a microsoftmvcjqueryvalidation. js

2. Add

<SCRIPT src = ".../scripts/jquery-1.4.1.js" type = "text/JavaScript"> </SCRIPT>

<SCRIPT src = ".../../scripts/jquery. Validate. js" type = "text/JavaScript"> </SCRIPT>
<SCRIPT src = ".../../scripts/microsoftmvcjqueryvalidation. js" type = "text/JavaScript"> </SCRIPT>

3. In index. aspx

<% Html. enableclientvalidation (); %>
<% Using (html. beginform () {%>
<%: HTML. editorformodel () %>
<Input type = "Submit" name = "Submit" value = "Submit"/>
<% }%>

 

 

Custom verification rules

1. Create an emailattribute class

Public class emailattribute: regularexpressionattribute {
Public emailattribute (): Base (
@ "^ [A-zA-Z0-9. _ % +-] + @(? : [A-zA-Z0-9-] + \.) + [A-Za-Z] {} $ "){
Errormessage = "email is invalid ";
}
}

2. models/person. CS:
Public datetime birthdate {Get; set ;}
[Datatype (datatype. emailaddress), required, email]
Public String email {Get; set ;}

3. Global. asax. CS:
Protected void application_start (){
Arearegistration. registerallareas ();
Registerroutes (routetable. routes );
Dataannotationsmodelvalidatorprovider. registeradapter (
Typeof (emailattribute ),
Typeof (regularexpressionattributeadapter ));
}

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.