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 ));
}