1

我有一个窗体,我成功地使用了[远程]注释的不显眼验证。 我在模型中有[需要]注释的表单中的其他字段,但我不希望这些字段的客户端验证。我可以将客户端验证限制到特定字段吗?

我只想为[必需]字段

我还没有找到一个解决方案,我不知道它是否容易可行的服务器端验证?

编辑:

我的代码

我的模型的一部分,部分:

我想第一场:“电子邮件”使用不引人注目的验证,第二个:“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验证。

+0

你有你已经尝试什么的例子吗? – davids

+0

是的,我用我的部分代码编辑我的文章 – Yoann

回答

0

可能会有更好的办法,但要做到这一点的一种方法是做到以下几点

@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> 
相关问题