- 爱易网页
-
ASP.NET教程
- MVC 验证的有关问题 应该不难
日期:2014-05-17 浏览次数:20499 次
MVC 验证的问题 应该不难
直接上代码,就是验证不触发。。。点submit以后,直接就到提交的方法了。
找了好多办法,惆怅。。。希望帮忙。
前台:
@model Expert.Models.Search_Expert
@{
ViewBag.Title = "专家抽取";
}
<script type="text/javascript" language="javascript">
</script>
<h2>Index</h2>
<table border="1">
<tr>
<td colspan="9">
@using (Html.BeginForm())
{
<fieldset>
抽取人数
@Html.EditorFor(model => model.Top)
@Html.ValidationMessageFor(model => model.Top)
,专业
@Html.DropDownListFor(model => model.Major, ViewData["listMajor"] as SelectList)
,回避单位
@Html.EditorFor(model => model.Company)
@Html.ValidationMessageFor(model => model.Company)
,抽取范围
@Html.DropDownListFor(model => model.Lv, ViewData["listLv"] as SelectList)
<input id="Submit1" type="submit" value="submit" />
</fieldset>
}</td>
</tr>
model:
public class Search_Expert
{//抽取人数<input id="txtTop" type="text" />,专业<select id="ddlMajor"></select>,回避单位<input id="txtCompany" type="text" />,抽取范围<select id="ddlLv"></select>
[DisplayName("抽取专家")]
[Required(ErrorMessage = "抽取专家人数不能为空")]
[Range(1, int.MaxValue, ErrorMessage = "专家人数必须大于等于1")]
public int Top { get; set; }
public string Major { get; set; }
[DisplayName("公司名称")]
[RegularExpression(@"/[0-9a-zA-Z\xa0-\xff]{1,32}/", ErrorMessage = "的格式不正确")]
[DataType(DataType.Text)]
public string Company { get; set; }
public string Lv { get; set; }
}
Controller:
public ActionResult Index()
{。。。。中间代码略。。。return View();}
[HttpPost]