由于比较简单,话不多说,直接上菜!
1.Employee类
public class Employee
{
public int Id { set; get; }
[StringLength(10,MinimumLength=10)]
public string Name { set; get; }
[RegularExpression("^[1-9][0-9]{0,1}$")]
public Nullable<int> Age { set; get; }
}
2.View
@model AspNetMvcModelValidate.Models.Employee
@using(@Html.BeginForm("Index","Home",FormMethod.Post))
{
<table>
<tr>
<td>Name</td>
<td>@Html.TextBoxFor(s=>s.Name)</td>
</tr>
<tr>
<td>Age</td>
<td>@Html.TextBoxFor(s => s.Age)</td>
</tr>
<tr>
<td colspan="2"><input type="submit" value="submit" style="width:215px" /></td>
</tr>
</table>
<hr />
@Html.ValidationSummary()
}
3.Controller
public ActionResult Index()
{
return View();
}
[HttpPost]
public ActionResult Index(Employee employee)
{
return View(employee);
}
4.Result