2012-12-05 241 views
2

我試圖將表單提交給控制器操作,並根據OnSuccess或OnFailure處理響應。問題是,即使數據無效並且我未通過ModelState.IsValid測試,也會調用OnSuccess方法。應該調用OnFailure方法。即使數據不好,Ajax.BeginForm也會觸發OnSuccess方法

筆者認爲:

@using (Ajax.BeginForm("UpdateCategory", "Home", null, new AjaxOptions { HttpMethod = "Post", UpdateTargetId = "categoryForm", OnSuccess = "alert('success');", OnFailure = "alert('failure');" }, new { id = "formEditCategory" })) 
{ 
    @Html.ValidationSummary(true) 

    @Html.HiddenFor(model => model.CategoryID) 

    <div> 
     <div class="editor-label"> 
      @Html.LabelFor(model => model.CategoryName) 
     </div> 
     <div class="small-multiline-field"> 
      @Html.EditorFor(model => model.CategoryName) 
     </div> 
     <div class="validationMsg"> 
      @Html.ValidationMessageFor(model => model.CategoryName) 
     </div> 
    </div> 
} 

我的控制器操作:

[HttpPost] 
public ActionResult UpdateCategory(CategoryVM category) 
{ 
    try 
    { 
     if (ModelState.IsValid) 
     { 
      var itemService = new ItemViewModels(); 
      itemService.UpdateCategory(category); 
     } 
    } 
    catch (DataException) 
    { 
     //Log the error (add a variable name after DataException) 
     ModelState.AddModelError("", "Unable to save changes. Try again, and if the problem persists, see your system administrator."); 
    } 

    return PartialView("EditCategoryInfo", category); 
} 

我的視圖模型:

public class CategoryVM 
{ 
    public int CategoryID { get; set; } 

    [StringLength(75, ErrorMessage = "Category Name must be under 75 characters.")] 
    [DataType(DataType.MultilineText)] 
    [Display(Name = "Name")] 
    public string CategoryName { get; set; } 

    [StringLength(3800, ErrorMessage = "Category Description must be under 3800 characters.")] 
    [DataType(DataType.MultilineText)] 
    [Display(Name = "Description")] 
    [AllowHtml] 
    public string CategoryDesc { get; set; } 

    [Display(Name = "Display on Web")] 
    public bool DisplayOnWeb { get; set; } 
} 

所以,如果我進入超過75個字符類別名稱字段中輸入字符串我可以看到表單未通過ModelState.IsValid測試,並且視圖被髮送回註釋「類別名稱必須少於75個字符」。錯誤信息。但不是觸發OnFailure事件,而是觸發OnSuccess事件。爲什麼?

在此先感謝。

回答

1

隨着一些更多的研究(和下面的盧卡斯的幫助),我發現this,這解決了我的問題。

+0

什麼可能已經取得了這樣的回答工作會一直,如果你已經張貼的解決方案,而不是一個鏈接到解決方案。只是一個小費。我贊成你,因爲我喜歡你鏈接的解決方案。 –

2

OnSuccess如果服務器返回成功的HTTP響應(例如, 200 OK,如果客戶端成功接收到響應並更新頁面(如MSDN中所述)

處理響應內容由您決定。

6

由於您已經發現了異常,因此您返回的是HTTP狀態碼爲200的PartialView。這就是OnSuccess被觸發的原因。

你能做些什麼,而不是響應代碼明確設置爲400(錯誤請求)

[HttpPost] 
public ActionResult UpdateCategory(CategoryVM category) 
{ 
    try 
    { 
     if (ModelState.IsValid) 
     { 
      var itemService = new ItemViewModels(); 
      itemService.UpdateCategory(category); 
     } 
    } 
    catch (DataException) 
    { 
     //Log the error (add a variable name after DataException) 
     ModelState.AddModelError("", "Unable to save changes. Try again, and if the problem persists, see your system administrator."); 
     Response.StatusCode = 400; 
    } 

    return PartialView("EditCategoryInfo", category); 
} 
+0

我正在使用'HttpStatusCodeResult'在一個場景中返回一個特定的代碼 - 一個在Firefox中因爲沒有內容被返回而中斷的代碼。我切換到使用'Response.StatusCode','Response.StatusDescription'和'Content(「 」)',它現在工作正常。 – Seth

+0

如果您將StatusCode設置爲錯誤,那麼您將不會收到驗證錯誤,並且您的頁面可能會發送到全局錯誤頁面。這不是我想要的解決方案。 –

+0

錯誤細節(在你的情況下,它是驗證錯誤)將作爲響應主體的一部分與狀態碼一起返回。設置狀態碼只會告訴你這個請求有問題。無論您想要將用戶發送到全局錯誤頁面還是在現有頁面上顯示錯誤消息,完全取決於您。你可以通過你的javascript來控制這種行爲。 – Jack

相關問題