我正在尝试验证登录页面上的用户输入。我有我User mode
的:
- 字符串用户名
- 字符串密码
我决定用部分类扩展我的数据库模型:
[MetadataType(typeof(User))]
public partial class User : IUser
{
}
public interface IUser
{
[Required(ErrorMessage = "Login is required")]
string UserName { get; set; }
[Required(ErrorMessage = "Password is required")]
string Password { get; set; }
}
我的view
:
@using (Html.BeginForm("Login", "Account", FormMethod.Post))
{
@Html.ValidationSummary(true)
@Html.TextBoxFor(model => model.UserName, new { size = 30, @placeholder = "Login",
@class = "credential" })
@Html.ValidationMessageFor(model => model.UserName)
@Html.PasswordFor(model => model.Password, new { size = 30, @placeholder =
"Password", @class = "credential" })
@Html.ValidationMessageFor(model => model.Password)
}
它不是客户端验证,而是在模型中的空值上进行回发和崩溃。
在这个问题之后我添加了
HtmlHelper.ClientValidationEnabled = true;
HtmlHelper.UnobtrusiveJavaScriptEnabled = true;
在视图顶部,确保已加载脚本:
<script src="@Url.Content("~/Scripts/jquery-1.8.3.min.js")"></script>
<script src="@Url.Content("~/Scripts/jquery.validate.min.js")"></script>
<script src="@Url.Content("~/Scripts/jquery.validate.unobtrusive.min.js")"></script>
我错过了什么?