2015-01-09 19 views
1

如果有人能提供建議,我很感激。來自控制器的調用窗體OnFailure處理程序,ASP.NET MVC

我有一個Ajax形式:

@using (Ajax.BeginForm("Edit", null, new AjaxOptions() { 
             UpdateTargetId = updateRegion, 
             InsertionMode = InsertionMode.Replace, 
             OnFailure = "formFailure" })) 
    {} 

UpdateTargetId不同基於當前的用戶角色:

@{ 
    if (curUser.IsInputer) 
    { 
     updateRegion = "content"; 
    } 
    else if (curUser.IsAuthorizer) 
    { 
     updateRegion = "mainPane"; 
    } 
} 

如果ModelState中是無效的,我想在mainPane總是返回看法:

<script> 
function formFailure(result) 
{ 
    $("#mainPane").html(result.responseText);   
} 
</script> 

但是onFailure不會在ModelSt時被調用吃了無效。對於這一點,我設置錯誤代碼控制器:

public ActionResult Edit(ContractModel entity) 
{ 
    if(ModelState.IsValid) 
    { 
     if(curUser.isInputer) { return RedirectToAction("SomeAction");} 
     if(curUser.Authorizer) { return RedirectToAction("OtherAction");} 
    } 

    Response.StatusCode = 500;//internal server error to fire OnFailure of form 
    return PartialView(entity); 
} 

然後我得到期望的結果,即我看到它的誤差mainPane格在瀏覽器控制檯模式和內部服務器錯誤。然而,當我在本地運行應用程序時,它會以這種方式工作,當我在服務器上發佈並運行應用程序時,我看到錯誤500 Internal server error,而不是局部視圖。有什麼解決方法嗎?

編輯: 作爲一個選項,我會嘗試檢查模型的形式onSuccess處理錯誤:

var isValid = @Html.Raw(Json.Encode(ViewData.ModelState.IsValid)); 

     if (!isValid) { 

      $("#mainPane").html(result.responseText);  
     } 

但我仍然得到「內容」分區的視圖。這是爲什麼發生?

回答

1

這是一個選項:

不使用500狀態錯誤。這是爲了報告你的應用程序出現了一些錯誤。 這不是發生在這裏,你只是有一個表單驗證錯誤。

更新您的服務器端代碼這樣:

public ActionResult Edit(ContractModel entity) 
{ 
    if(ModelState.IsValid) 
    { 
     if(curUser.isInputer) { return RedirectToAction("SomeAction");} 
     if(curUser.Authorizer) { return RedirectToAction("OtherAction");} 
    } 

    if(!ModelState.IsValid) 
    { 
     entity.FormError = true; // you will need to add this attribute to your model 
    } 

    return PartialView(entity); 
} 

然後在你的部分觀點,把這樣的事情:

if(Model.FormError) 
{ 
    @Html.HiddenFor(m => m.FormError) 
} 

更改您的形式來處理的onComplete事件,而不是onFailure處的事件。這是因爲你的ajax文章沒有失敗。它成功地返回和報告有一個與形式的輸入驗證錯誤:

@using (Ajax.BeginForm("Edit", null, new AjaxOptions() { OnSuccess = "handleFormResponse" })) 
    {} 

更改處理程序腳本這樣的事情。這將檢查響應,看看是否包含表單錯誤

<script> 
    function handleFormResponse(result) 
    { 
     $(result).find("#FormError").length > 0) 
     { 
      $("#mainPane").html(result); 
     } 
     else 
     { 
      $("#@updateRegion").html(result); 
     } 

    } 
    </script> 
+0

感謝您的回覆,但其工作方式與我上次修改時的方式相同。我在「內容」div中看到了responseText。 –

+0

我不這麼認爲。您正在使用:var isValid = @ Html.Raw(Json.Encode(ViewData.ModelState.IsValid))設置「isValid」ONCE,當頁面首次加載時。 ajax表單不會重新加載頁面,因此該行不會再被執行。按照上面的示例,您需要在調用ajax之後再次將ModelState.IsValid發送回客戶端。 – Keith

+0

好的,同意。我試過你的答案,它不適合我,如果formError爲真我仍然獲得「content」div中的所有內容。 –

0

這是可能發生的,因爲服務器解釋500作爲unhanded錯誤,因爲在web配置,當你發佈在釋放模式的應用有可能被撞飛到falsecustomErrors設置恢復默認500錯誤。

編輯:刪除提及使用另一個狀態代碼,因爲它造成更多的困惑,並不能解決問題。

+0

我仍然看到錯誤「400錯誤請求」。我認爲設置Response.StatusCode不是一個選項,我應該看到具有錯誤的模型而不是錯誤消息。 –

+0

根本不需要響應代碼,即使沒有它,也應該顯示模型錯誤,如果我沒有弄錯,那麼由於'InsertionMode'參數的javascript方法。是否有你想使用響應代碼的原因? – Jarga

+0

是的,我知道我可以看到沒有響應代碼的模型錯誤,但我的問題是,我使用基於當前用戶角色的不同UpdateTargetIds。但是如果模型返回一些錯誤,我需要留在「mainPane」div中,而不管角色。 –

相關問題