我对Html.ValidationSummary有一些问题。我不想在ValidationSummary中显示属性错误。当我设置Html.ValidationSummary(true)时,它不会显示来自ModelState的错误消息。当对字符串的控制器操作中出现某些异常时
MembersManager.RegisterMember(member);
catch部分将错误添加到ModelState:
ModelState.AddModelError("error", ex.Message);
但是ValidationSummary不会显示此错误消息。当我设置Html.ValidationSummary(false)时,将显示所有消息,但我不想显示属性错误。我该如何解决这个问题?
这是我正在使用的代码:
模型:
public class Member
{
[Required(ErrorMessage = "*")]
[DisplayName("Login:")]
public string Login { get; set; }
[Required(ErrorMessage = "*")]
[DataType(DataType.Password)]
[DisplayName("Password:")]
public string Password { get; set; }
[Required(ErrorMessage = "*")]
[DataType(DataType.Password)]
[DisplayName("Confirm Password:")]
public string ConfirmPassword { get; set; }
}
控制器:
[HttpPost]
public ActionResult Register(Member member)
{
try
{
if (!ModelState.IsValid)
return View();
MembersManager.RegisterMember(member);
}
catch (Exception ex)
{
ModelState.AddModelError("error", ex.Message);
return View(member);
}
}
视图:
<% using (Html.BeginForm("Register", "Members", FormMethod.Post,
new { enctype = "multipart/form-data" })) {%>
<p>
<%= Html.LabelFor(model => model.Login)%>
<%= Html.TextBoxFor(model => model.Login)%>
<%= Html.ValidationMessageFor(model => model.Login)%>
</p>
<p>
<%= Html.LabelFor(model => model.Password)%>
<%= Html.PasswordFor(model => model.Password)%>
<%= Html.ValidationMessageFor(model => model.Password)%>
</p>
<p>
<%= Html.LabelFor(model => model.ConfirmPassword)%>
<%= Html.PasswordFor(model => model.ConfirmPassword)%>
<%= Html.ValidationMessageFor(model => model.ConfirmPassword)%>
</p>
<div>
<input type="submit" value="Create" />
</div>
<%= Html.ValidationSummary(true)%>
<% } %>