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> 
相關問題