我有一个简单的强类型视图。
@model GoldForGold.Models.LogonModel
@{
ViewBag.Title = "Logins";
Layout = "~/Views/Shared/_Layout.cshtml";
}
Logins
@using (Html.BeginForm()) {
Account Information
@Html.LabelFor(m => m.UserName)
@Html.TextBoxFor(m => m.UserName, new { id = "txtUserName" })
@Html.ValidationMessageFor(m => m.UserName)
@Html.LabelFor(m => m.Password)
@Html.PasswordFor(m => m.Password, new { id = "txtPassword" })
@Html.ValidationMessageFor(m => m.Password)
@Html.CheckBoxFor(m => m.RememberMe)
@Html.LabelFor(m => m.RememberMe)
<input type="submit" value="Log On" onclick="getcredentials()" />
}
型号代码在这里。
public class LogonModel
{
[Required(ErrorMessage="please enter username")]
[Display(Name = "User name")]
public string UserName { get; set; }
[Required]
[DataType(DataType.Password)]
[Display(Name = "Password")]
public string Password { get; set; }
[Display(Name = "Remember me?")]
public bool RememberMe { get; set; }
}
即使不输入用户名和密码,我也没有任何反应。
对于客户端验证,您需要jquery和jquery验证。
之后,必须在config中启用客户端验证:
<appSettings>
...
<add key="ClientValidationEnabled" value="true" />
<add key="UnobtrusiveJavaScriptEnabled" value="true" />
对于服务器端验证,您可以使用以下方法检查验证状态:
ModelState.IsValid;
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句