2016-08-02 95 views
1

這裏是我的控制器操作:MVC Ajax.BeginForm不顯示驗證錯誤

[HttpPost] 
     public ActionResult CreateModal(MyModel myModel) 
     { 
      if (ModelState.IsValid) 
      { 
       //success logic 
      } 
      return PartialView("_MyPartial", myModel); 

     } 

而我的部分(這是在一個jQuery對話框):

@using (Ajax.BeginForm("CreateModal", "MyController", new {area = "MyArea2"}, new AjaxOptions() {HttpMethod = "POST", InsertionMode = InsertionMode.Replace})) 
    { 
     @Html.AntiForgeryToken() 
     @Html.ValidationSummary(true, "", new {@class = "text-danger"}) 
     <div class="row"> 
      <fieldset> 
       <legend>Info</legend> 
       <div class="row"> 
        <div class="form-group col-md-4"> 
         @Html.LabelFor(model => model.Name, htmlAttributes: new {@class = "control-label"}) 
         @Html.EditorFor(model => model.Name, new {htmlAttributes = new {@class = "form-control"}}) 
         @Html.ValidationMessageFor(model => model.Name, "", new {@class = "text-danger"}) 
        </div> 
       </div> 
       </fieldset> 
       </div> 
       } 

我可以張貼到我的行動就好了,但是如果我的ModelState無效,那麼即使單步調試器出現錯誤,我也不會在return PartialView("_MyPartial", myModel)上發生驗證錯誤。

我正在使用最新版本的jquery和來自nuget的相關技術。

此外,不知道它是否重要,但我從MyArea1張貼到控制器MyArea2_MyPartial住在一個共同的文件夾中。

+1

您沒有在'AjaxOptions'指定了'UpdateTargetId'(和你們爲什麼不實現不顯眼的客戶端驗證,以防止提交表單如果無效?) –

+0

如果我要返回整個分部視圖,則我不需要UpdateTargetId,因此我的印象不佳。 – RJP

+0

@StephenMuecke如何實現不顯眼的客戶端驗證? – Alex

回答

0
AjaxOptions(){ 
    ... 
    OnComplete = "ParsUnob" 
    ... 
} 

腳本代碼:

function ParsUnob() { 
    $.validator.unobtrusive.parse("form"); 
}