脚本(包含在下面的顺序中):
/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
型号:
公共类用户 { [键] [ScaffoldColumn(假)] 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=""/>
奇怪的是找不到远程验证属性。我真的很难解决这个问题。我不知道出了什么问题。请帮帮我。