通过模型绑定获取表单数据
模型绑定是将浏览器发送的HTTP请求数据转换为.NET对象的过程 模型绑定使我们在控制器中可以直接获取视图,或URL传递过来的数据,并且这些数据可以自动转换为模型对象,以便调用
有四种途径可以获取绑定的值,其顺序如下: (1)Request.Form:获取表单提交的值; (2)RouteData.Values:获取路由的值; (3)Request.QueryString:获取URL的值; (4)Request.Files:获取上传文件。
手动获取Form表单数据
表单页面
<body>
<table>
<tr> <td> <h1>个人信息</h1></tr> </td>
<tr>
@using(Html.BeginForm())
{
<th>ID :</th>
<td>@Html.TextBox("id")</td>
<th>姓 :</th>
<td>@Html.TextBox("FirstName")</td>
<th>名 :</th>
<td>@Html.TextBox("LastName")</td>
<td><input type="submit" value="提交"></td>
@ViewBag.StatusMessage
}
</tr>
</table>
</body>
Person类
public class Person
{
//证件Id
public string Id { get; set; }
//姓
public string FirstName { get; set; }
//名
public string LastName { get; set; }
}
Home控制器里的Index方法
public class HomeController : Controller
{
public ActionResult Index()
{
Person p = new Person();
if (Request.Form.Count > 0)
{
p.Id = Request.Form["Id"];
p.FirstName = Request.Form["FirstName"];
p.LastName = Request.Form["LastName"];
}
return View();
}
//post请求后执行的Index方法
[HttpPost]
public ActionResult Index(Person p)
{
ViewBag.StatusMessage = "欢迎您!" + p.FirstName + p.LastName + " ,您的证件号是"
+ p.Id + "!";
return View();
}
}
运行结果
通过绑定基本类型获取表单数据
表单页面
<body>
<table>
<tr>
@using(Html.BeginForm())
{
<th>商品编号 :</th>
<td>@Html.TextBox("gid")</td>
<th>商品名称 :</th>
<td>@Html.TextBox("gname")</td>
<td><input type="submit" value="提交"></td>
@ViewBag.Info
}
</tr>
</table>
</body>
Home控制器里的Index方法
public class HomeController : Controller
{
public ActionResult Index()
{
return View();
}
//post请求后执行的Index方法
//int? gid的意思是商品的gid可写可不写
//在“ Index(string gname, int? gid)”方法的参数声明中,gid参数需要设定为int?类型,这种类型称为“可
//空 int类型”,表示即可以是int类型,也可以是null类型。
[HttpPost]
public ActionResult index(string gname, int? gid)
{
ViewBag.Info = "您输入的商品编号是:" +gid + ",名称是:" + gname;
return View();
}
}
运行结果
对模型数据进行验证
ASP.NET MVC框架提供了数据验证的API,称为“模型数据注解”或“模型元数据” 数据验证API位于System.ComponentModel.DataAnnotations 名称空间
数据验证API | 示例 | 说明 |
Compare | [Compare("MyOtherProperty")] | 两个属性值必须相同 |
Range | [Range(10, 20)] | 属性值必须在指定的数值范围内 |
RegularExpression | [RegularExpression("pattern")] | 属性值必须匹配正则表达式 |
Required | [Required] | 属性值必须非空或者不能只是空格 |
StringLength | [StringLength(10)] | 属性值长度不能超过给定的最大长度 |
Movie类
public class Movie
{
//“[Required]”不允许Title属性值为空
[DisplayName("影片名称")]
[Required(ErrorMessage = "{0}不能为空")]
public string Title { get; set; }
//“[DataType]”指定了ReleaseDate属性值为日期格式
[DisplayName("上映日期")]
[DataType(DataType.Date, ErrorMessage = "{0}的日期格式不对")]
public DateTime ReleaseDate{ get; set; }
//“[Range]”限制了Price属性值在1~100范围
[DisplayName("光盘价格")]
[Range(1, 100, ErrorMessage = "{0}必须在1-100之间")]
//“[DataType]”指定了货币格式
[DataType(DataType.Currency)]
public decimal Price{ get; set; }
//[StringLength]约束了Rating属性值的最大长度是10
[DisplayName("评分")]
[StringLength(10, ErrorMessage = "{0}必须在10个字以内")]
public string Rating{ get; set; }
[RegularExpression("^\\s*([A-Za-z0-9_-]+(\\.\\w+)*@(\\w+\\.)+\\w{2,5})\\s*$", ErrorMessage ="{0}邮箱格式不正确")]
public string userEmail { get; set; }
}
表单页面
<div>
<h1 style="text-align:center">添加电影信息</h1>
@using (Html.BeginForm())
{
<table align="center" cellspacing="0" border="1">
<tr>
<td>影片名称:</td>
<td>@Html.TextBox("Title")</td>
</tr>
<tr>
<td>上映时间:</td>
<td>@Html.TextBox("ReleaseDate")</td>
</tr>
<tr>
<td>光盘价格:</td>
<td>@Html.TextBox("Price")</td>
</tr>
<tr>
<td>评分:</td>
<td>@Html.TextBox("Rating")</td>
</tr>
<tr>
<td>邮箱</td>
<td>@Html.TextBox("userEmail")</td>
</tr>
<tr>
<td colspan="2" align="center"><input type="submit" name="name" value="添加" /></td>
</tr>
</table>
}
@Html.ValidationSummary()
@ViewBag.Info
</div>
运行结果