脚本(按以下顺序包含):
/Scripts/jquery-1.10.2.min.js
/Scripts/jquery-ui-1.10.3.min.js
/Scripts/jquery.validate.min.js
/Scripts/jquery.validate.unobtrusive.min.js
模型:
公共类用户 { [Key] [ScaffoldColumn(false)] public Guid UserID { get; 放; }
[Required(AllowEmptyStrings = false, ErrorMessage = "Username required.")]
[StringLength(30, MinimumLength = 5, ErrorMessage = "Min: 5, Max: 30 characters")]
[Remote("IsUsername_Available", "Users", AdditionalFields = "Email", HttpMethod="Post", ErrorMessage = "Username already exists.")]
public string Username { get; set; }
[Required(AllowEmptyStrings = false, ErrorMessage = "Password required.")]
[StringLength(30, MinimumLength = 5, ErrorMessage = "Min: 5, Max: 30 characters")]
[DataType(DataType.Password)]
public string Password { get; set; }
}
看法:
@using (Ajax.BeginForm("Create",null,new AjaxOptions(){ UpdateTargetId="form1" }, new { id = "form1", @class = "small-forms" }))
{
<fieldset>
<legend>User Registration</legend>
<div class="form-row">
<div class="col1">@Html.LabelFor(m => m.Users.Username)</div>
<div class="col2">@Html.TextBoxFor(m => m.Users.Username, new { @class = "textbox" })</div>
@Html.ValidationMessageFor(m => m.Users.Username)
</div>
<div class="form-row">
<div class="col1">@Html.LabelFor(m => m.Users.Password)</div>
<div class="col2">@Html.PasswordFor(m => m.Users.Password, new { @class = "textbox" })</div>
@Html.ValidationMessageFor(m => m.Users.Password)
</div>
</fieldset>
}
控制器:
public JsonResult IsUsername_Available(string Username, string Email)
{
return Json(false, JsonRequestBehavior.AllowGet);
}
为用户名生成的 HTML 输入标记:
<input name="Users.Username" class="text-box single-line" id="Users_Username" type="text" data-val-required="Username required." data-val-length-min="5" data-val-length-max="30" data-val-length="Min: 5, Max: 30 characters" data-val="true" value=""/>
奇怪的是找不到远程验证属性。我真的很难解决这个问题。我不知道哪里出了问题。请帮我。