我有一个表单,在这个表单中,我成功地使用了带远程注释的unobtrusive-validation。我在表单中的其他字段在模型中有必需的注释,但我不希望这些字段在客户端进行验证。
我只需要对必填字段进行服务器端验证
我还没有找到一个解决方案,我想知道它是否容易可行?
编辑:
我的代码的一部分
我的模型的一部分:
我想要第一个字段:"Email“使用不显眼的验证,第二个字段:"PasswordHash”只使用服务器端验证,即使它需要注释。
最后,我不希望所有的表单字段都出现AJAX错误消息。
[Required(ErrorMessageResourceType = typeof(Project.Resources.Models.Accounts.User), ErrorMessageResourceName = "EMAIL_REQUIRED")]
[Display(Name = "EMAIL_DISPLAY", ResourceType = typeof(Project.Resources.Models.Accounts.User))]
[Remote("EmailExists", "User", "An account with this email address already exists.")]
public string Email { get; set; }
[Required(ErrorMessageResourceType = typeof(Project.Resources.Models.Accounts.User), ErrorMessageResourceName = "PASSWORDHASH_REQUIRED")]
[DataType(DataType.Password)]
[Display(Name = "PASSWORDHASH_DISPLAY", ResourceType = typeof(Project.Resources.Models.Accounts.User))]
public string PasswordHash { get; set; }控制器服务器端验证中的部分操作:
[HttpPost]
public ActionResult Register(User user)
{
if (ModelState.IsValid )
{
DataContext.User.Add(user);
DataContext.SaveChanges();
return RedirectToAction("Index");
}
return View(user);
}Ajax验证:
public JsonResult EmailExists(string email)
{
User user = DataContext.User.SingleOrDefault(x => x.Email == email);
return user == null ?
Json(true, JsonRequestBehavior.AllowGet) :
Json(
string.Format("an account for address {0} already exists.",
email), JsonRequestBehavior.AllowGet);
}视图的一部分:
@using (Html.BeginForm())
{
@Html.ValidationSummary(true)
<div class="editor-label">
@Html.LabelFor(model => model.Email)
</div>
<div class="editor-field">
@Html.EditorFor(model => model.Email)
@Html.ValidationMessageFor(model => model.Email)
</div>
<div class="editor-label">
@Html.LabelFor(model => model.PasswordHash)
</div>
<div class="editor-field">
@Html.EditorFor(model => model.PasswordHash)
@Html.ValidationMessageFor(model => model.PasswordHash)
</div>
<p>
<input type="submit" name="op" id="edit-submit" value="@Project.Resources.Views.User.Register.SUBMIT_FORM" class="form-submit art-button" />
</p>
}当我点击sumbit按钮时,我想要一个服务器端验证。
对于一些特定的领域,如电子邮件,我想要一个Ajax验证。
发布于 2012-11-11 06:17:17
可能有更好的方法,但实现这一点的一种方法是执行以下操作
@using (Html.BeginForm("Register", "Home", FormMethod.Post, new { id = "registerForm" }))
{
@Html.ValidationSummary(true)
<div class="editor-label">
@Html.LabelFor(model => model.Email)
</div>
<div class="editor-field">
@Html.EditorFor(model => model.Email)
@Html.ValidationMessageFor(model => model.Email)
</div>
<div class="editor-label">
@Html.LabelFor(model => model.PasswordHash)
</div>
<div class="editor-field">
@Html.EditorFor(model => model.PasswordHash)
@Html.ValidationMessageFor(model => model.PasswordHash)
</div>
<p>
<input type="submit" name="op" id="edit-submit" value="@Project.Resources.Views.User.Register.SUBMIT_FORM" class="form-submit art-button" />
</p>
}
<script type="text/javascript">
// add the rules for the fields that you want client-side validation on
// leave out the fields that you dont want client-side validation. they will be validated
// on server side.
$("#registerForm").validate({
rules: {
Email: { required: true }
},
messages: {
Email: { required : "Email is required" }
}
});
</script>https://stackoverflow.com/questions/13317414
复制相似问题