我正在尝试将Foundation 5与验证挂钩到我的MVC 5视图。当我离开必填字段并尝试提交表单时,我看到所有必填字段都以红色突出显示,但我想看到错误消息我在Required中添加了C#视图模型(ErrorMessage =“username is required”]
这些是我已添加的内容 将这些密钥添加到我的根级别web.config
<add key="ClientValidationEnabled" value="true" />
<add key="UnobtrusiveJavaScriptEnabled" value="true" />
将data_abide属性添加到表单元素
@using (Html.BeginForm("Register", "Account", FormMethod.Post, new { id = "form-user-register", data_abide = "" }))
{
<div class="row">
<div class="small-3 columns">
@Html.DisplayNameFor(m => m.LoginName)<br />
@Html.TextBoxFor(m => m.LoginName, new { id = "register-loginname"})
</div>
</div>
<div class="row">
<div class="small-3 columns">
@Html.DisplayNameFor(m => m.UserPassword)<br />
@Html.TextBoxFor(m => m.UserPassword, new { id = "register-loginpassword" })
</div>
</div>
<div class="row">
<div class="small-3 columns">
@Html.DisplayNameFor(m => m.Email)<br />
@Html.TextBoxFor(m => m.Email, new { id = "register-loginpassword" })
</div>
</div>
<div class="row">
<div class="small-3 columns">
@Html.DisplayNameFor(m => m.FirstName)<br />
@Html.TextBoxFor(m => m.FirstName, new { id = "register-login-firstname" })
</div>
</div>
<div class="row">
<div class="small-3 columns">
@Html.DisplayNameFor(m => m.LastName)<br />
@Html.TextBoxFor(m => m.FirstName, new { id = "register-login-firstname" })
</div>
</div>
<div class="row">
<div class="small-12 columns">
<div class="left">
<input type="submit" class="button radius small right" value="Register" />
</div>
</div>
</div>
}
我确保将jquery.validate.unobtrusive.js和jquery.validate.js添加到_LayoutView.cshtml
这是C#Viewmodel
public class RegisterViewModel
{
public int AppUserId { get; set; }
[Display(Name="Username")]
[Required(ErrorMessage="Username is required")]
public string LoginName { get; set; }
[Display(Name = "Email")]
[Required(ErrorMessage = "Email is required")]
public string Email { get; set; }
[Display(Name = "FirstName")]
[Required(ErrorMessage = "FirstName is required")]
public string FirstName { get; set; }
[Display(Name = "LastName")]
[Required(ErrorMessage = "LastName is required")]
public string LastName { get; set; }
[Display(Name = "Password")]
[Required(ErrorMessage = "Password is required")]
public string UserPassword { get; set; }
public AddressViewModel Address { get; set; }
}
我仍然只是上面的截图