2014-11-04 25 views
0

我有一部分是通過Ajax刷新的。ASP.NET MVC:在部分主視圖中重定向錯誤

查看使用Javascript:

$("#SelectedId").change(function() { 
    var id = $("#SelectedId").val(); 
    if (id > 0) { 
    $.ajax({ 
     url: "/Home/Refresh", 
     data: { id: id }, 
     type: "POST", 
     success: function (result) { 
     $('#partialHolder').html(result); 
     } 
    }); 
    } 
}); 

刷新動作:

[HttpPost] 
public ActionResult Refresh(int id) 
{ 
    HomeViewModel model = new HomeViewModel(); 
    model.id = id; 

    ViewBag.id = model.id; 
    PrepareModel(ref model); 
    return PartialView("~/Views/PartialView.cshtml", model); 
} 

這種運作良好,所不同的是當一個錯誤(諸如HTTP異常)時,錯誤視圖被髮送到局部當我希望它被顯示爲主視圖時。

錯誤動作:

public ActionResult Error(string message) 
{ 
    ViewBag.Message = message; 
    return View(); 
} 

我已經使用Javascript(無論是在視圖和控制器返回)將瀏覽器重定向嘗試,但都是車,我認爲不好的做法。

+1

您可以顯示操作刷新的代碼? – theLaw 2014-11-04 14:38:07

+0

請參閱:http://stackoverflow.com/questions/2261617/how-to-handle-model-state-errors-in-ajax-invoked-controller-action-that-returns – ScottE 2014-11-04 14:49:56

回答

0

我設法解決這個通過使用一個類似於theLaw's answer的方法嘗試/ catch塊和Content輔助方法返回一個簡單的字符串,觸發重定向。

刷新操作:

[HttpPost] 
public ActionResult Refresh(int id) 
{ 
    try 
    { 
     HomeViewModel model = new HomeViewModel(); 
     model.id = id; 

     ViewBag.id = model.id; 
     PrepareModel(ref model); 
     return PartialView("~/Views/PartialView.cshtml", model); 
    } 
    catch 
    { 
     return Content("error"); 
    } 
} 

查看使用Javascript:

$("#SelectedId").change(function() { 
    var id = $("#SelectedId").val(); 
    if (id > 0) { 
    $.ajax({ 
     url: "/Home/Refresh", 
     data: { id: id }, 
     type: "POST", 
     success: function (result) { 
     if (result == "error") { 
      location.replace("@Url.Action("General", "Error")"); 
     } 
     else { 
      ('#partialHolder').html(result); 
     } 
     } 
    }); 
    } 
}); 
0

嘗試爲

$("#SelectedId").change(function() { 
    var id = $("#SelectedId").val(); 
    if (id > 0) { 
    $.ajax({ 
     url: "/Home/Refresh", 
     data: { id: id }, 
     type: "POST", 
     success: function (result) { 
     $('#partialHolder').html(result); 
     }, 
     error: function(result){ 
     //call new action 
     } 
    }); 
    } 
}); 
+0

有關的例外似乎並不會影響部分數據的返回,只有部分數據會返回 - 所以錯誤數據塊永遠不會運行。 – 2014-11-04 15:33:03

0

假設Error()返回完整視圖(與佈局和腳本/ CSS),你總是可以嘗試完全覆蓋您的文檔:

success: function (result) { 
    $('#partialHolder').html(result); 
}, 
error: function(xhr) { 
    if(xhr.responseText) { 
     var newDoc = document.open("text/html", "replace"); 
     newDoc.write(xhr.responseText); 
     newDoc.close(); 
    } 
} 

這將有效地覆蓋整個DOM和導致你失去了頁面上的所有內容。

學分文件重寫this answer

+0

與Amit一樣 - 出於某種原因,即使拋出異常,錯誤塊也不會運行。 – 2014-11-04 15:34:14

+0

@OliWalker你的服務器正在返回什麼?它返回哪個http狀態碼? – Kippie 2014-11-04 15:36:42

+0

部分視圖正在返回,但其中的調用不成功並導致錯誤。這個問題很快就會解決,但我想確保將來的錯誤顯示在主視圖中。 – 2014-11-04 15:46:16

0

嘗試是這樣的

在你的行動

[HttpPost] 
public ActionResult Refresh(int id) 
{ 
    try 
    { 
     HomeViewModel model = new HomeViewModel(); 
     model.id = id; 

     ViewBag.id = model.id; 
     PrepareModel(ref model); 
     return PartialView("~/Views/PartialView.cshtml", model); 
    } 
    catch 
    { 
     return PartialView("~/Views/PartialView.cshtml", null); 
    } 
} 

所以在你的Ajax成功

if (result != null) { 
    //do your stuff 
} else { 
    // the controller action returned a partial 
    $('#divInYourMain').html("Error 123"); 
} 
+0

不幸的是,這也不起作用。我沒有看到如何返回一個null模型的部分會使'result == null'爲真? – 2014-11-04 17:18:48

相關問題