我正在使用 MVC4。
我有一个用[Required()]
验证属性装饰的 ViewModel。
提交表单时,客户端验证被触发并在线显示(使用验证摘要),因此一切正常。
如何在对话框中而不是内联中显示验证?
这是来自 MVC 4 Internet 应用程序模板的示例代码
public class RegisterModel
{
[Required]
[Display(Name = "User name")]
public string UserName { get; set; }
[Required]
[StringLength(100, ErrorMessage = "The {0} must be at least {2} characters long.", MinimumLength = 6)]
[DataType(DataType.Password)]
[Display(Name = "Password")]
public string Password { get; set; }
[DataType(DataType.Password)]
[Display(Name = "Confirm password")]
[Compare("Password", ErrorMessage = "The password and confirmation password do not match.")]
public string ConfirmPassword { get; set; }
}
@model MvcApplication2.Models.RegisterModel
@{
ViewBag.Title = "Register";
}
<hgroup class="title">
<h1>@ViewBag.Title.</h1>
<h2>Create a new account.</h2>
</hgroup>
@using (Html.BeginForm()) {
@Html.AntiForgeryToken()
@Html.ValidationSummary()
<fieldset>
<legend>Registration Form</legend>
<ol>
<li>
@Html.LabelFor(m => m.UserName)
@Html.TextBoxFor(m => m.UserName)
</li>
<li>
@Html.LabelFor(m => m.Password)
@Html.PasswordFor(m => m.Password)
</li>
<li>
@Html.LabelFor(m => m.ConfirmPassword)
@Html.PasswordFor(m => m.ConfirmPassword)
</li>
</ol>
<input type="submit" value="Register" />
</fieldset>
}
@section Scripts {
@Scripts.Render("~/bundles/jqueryval")
}
因此,当我在文本框中没有任何数据的情况下单击提交按钮时,错误消息会显示在验证摘要的顶部。
目前我正在使用 Kendo ui 对话框来显示服务器返回的所有错误。所以我要做的是统一错误消息,以便它们都以类似的方式显示。
目前我只想知道如何挂钩到客户端验证,所以我可以调用带有错误详细信息的 Kendo ui 对话框。
谢谢