在我进入细节之前,这就是我所拥有的
环境:VS 2008 与 ASP.NET MVC 2
基本型号代码
[DisplayName("Current Application Status")]
[Required(ErrorMessage = "Current Status has to be provided")]
public virtual int CurrStatus{ get; set; }
脚本参考
<link href="../../Content/Site.css" rel="stylesheet" type="text/css" />
<link href="../../Content/jqueryui.css" rel="stylesheet" type="text/css" />
<script src="../../Scripts/jquery-1.9.1.js" type="text/javascript"></script>
<script src="../../Scripts/jquery-ui-1.10.3.custom.js" type="text/javascript"></script>
<script src="../../Scripts/jquery.validate.js" type="text/javascript"></script>
<script src="../../Scripts/jquery.validate.unobtrusive.js" type="text/javascript"></script>
查看代码
<tr>
<td><%= Html.LabelFor(model => model.CurrStatus) %></td>
<td>
<%= Html.DropDownListFor(model => model.CurrStatus, Model.CurrentStatus, "-- Select Current Status --").ToString().Replace("CurrStatus", "Application.CurrStatus")%>
<%=Html.ValidationMessageFor(model=>model.CurrStatus) %>
</td>
</tr>
控制器代码
[AcceptVerbs(HttpVerbs.Post)]
public ActionResult Create([Bind(Prefix = "Application")] Application model)
{
try
{
if (ModelState.IsValid)
{
//save to db
}
else
{
InitSelectListItems();
model.CurrentStatus = CurrentStatusModel.GetModelInstance().GetAllAsSelectListItems();
return View(model);
}
return RedirectToAction("Index");
}
catch
{
return View();
}
}
在视图中,验证摘要显示已提交模型的错误列表。但是,由于某种原因,未显示单个错误消息。最重要的是,在将条目添加到 web.config 并将 jQUery 脚本添加到脚本文件夹后,我启用了客户端验证。客户端验证也没有被触发。对此的任何帮助将不胜感激。