欢迎来到DIVCSS5查找CSS资料与学习DIV CSS布局技术!
  首先我们在M层创建一个类:
 
  
 
  usingSystem;
 
  usingSystem.Collections.Generic;
 
  usingSystem.ComponentModel.DataAnnotations;
 
  usingSystem.Linq;
 
  usingSystem.Web;
 
  namespaceWebApplication1.Models
 
  {
 
  publicclassGuestResponse
 
  {
 
  //数据验证,必填项,如果不填ErrorMessage请输入你的用户名
 
  [Required(ErrorMessage="请输入你的用户名!")]
 
  publicstringName{get;set;}
 
  //同上
 
  [Required(ErrorMessage="请输入邮箱")]
 
  //正则表达式,判断是否是邮箱格式
 
  [RegularExpression(".+\\@.+\\..+",
 
  ErrorMessage="请输入正确的邮箱格式")]
 
  publicstringEmail{get;set;}
 
  //同上
 
  [Required(ErrorMessage="请输入你的手机号码")]
 
  publicstringPhone{get;set;}
 
  publicbool?WillAttend{get;set;}
 
  }
 
  }
 
  代码中已有注释,不多说。
 
  下面,V层:
 
  
 
  @modelWebApplication1.Models.GuestResponse
 
  @{
 
  Layout=null;
 
  }
 
  <!DOCTYPEhtml>
 
  <html>
 
  <head>
 
  <metaname="viewport"content="width=device-width"/>
 
  <title>RsvpForm</title>
 
  </head>
 
  <body>
 
  @using(Html.BeginForm())
 
  {
 
  @Html.ValidationSummary()
 
  <p>Yourname:@Html.TextBoxFor(x=>x.Name)</p>
 
  <p>Youremail:@Html.TextBoxFor(x=>x.Email)</p>
 
  <p>Yourphone:@Html.TextBoxFor(x=>x.Phone)</p>
 
  <p>
 
  Willyouattend?
 
  @Html.DropDownListFor(x=>x.WillAttend,new[]{
 
  newSelectListItem(){Text="Yes,I'llbethere",
 
  Value=bool.TrueString},
 
  newSelectListItem(){Text="No,Ican'tcome",
 
  Value=bool.FalseString}
 
  },"Chooseanoption")
 
  </p>
 
  <inputtype="submit"value="SubmitRSVP"/>
 
  }
 
  </body>
 
  </html>
 
  这里注意第一行,
 
  @modelWebApplication1.Models.GuestResponse
 
  我们绑定我们写的数据类,这样我们才能顺利创建表单。
 
  然后是C层:
 
  
 
  [HttpGet]
 
  publicViewResultRsvpForm(){
 
  returnView();
 
  }
 
  [HttpPost]
 
  publicViewResultRsvpForm(GuestResponsemodel)
 
  {
 
  if(ModelState.IsValid)
 
  {
 
  //TODO:Emailresponsetothepartyorganizer
 
  returnView("Thanks",model);
 
  }
 
  else
 
  {
 
  //thereisavalidationerror
 
  returnView();
 
  }
 
  }
 
  这里我们有两个RsvpForm,我们在上面添加提交方式,分别是Get和Post
 
  if(ModelState.IsValid)
 
  是否通过数据验证,通过返回视图Thanks,且把表单传过来的值传给Thanks视图
 
  数据验证不通过,返回原视图,
 
  这里注意V层
 
  @Html.ValidationSummary()
 
  添加这句话,在数据验证的时候,数据不通过,视图层才能显示我们的错误提示信息。






本文转载自中文网

 

如需转载,请注明文章出处和来源网址:http://www.divcss5.com/html5/h54957.shtml