我想對我的行動的呼籲,並有行動要麼返回直接渲染到視圖或有動作所產生的局部視圖重定向到另一頁在服務器上。調用從jQuery的MVC動作和處理重定向或返回的局部視圖
然而,因爲我通過jQuery這樣做似乎重定向的頁面加載到我的目標div元素,而不是重定向乾淨,有效地重新加載頁面/網站。
jQuery的電話:
$.ajax({
type: "GET",
url: "Myurl",
dataType: "html",
success: function (data) {
// replace the context of the section with the returned partial view
$('#upload_section').html(data);
}
});
MVC動作例如
public ActionResult MyAction()
{
bool doRedirect = // some code to determine this condition
if (doRedirect)
{
return RedirectToAction("MyAction", "Home");
}
else
{
// return the partial view to be shown
return PartialView("_UploadSessionRow");
}
}
我這樣做都是錯的?有沒有更好的做法呢?有必要做,所以I''m尋找如何去這一個共同的方法將發生在其他動作和jQuery請求。
更新: 感謝安德魯斯答案我得到了什麼後,通過改變我的ajax,根據他的建議進行了一些修改。最終阿賈克斯是:
function loadOrRedirect(options) {
var jData = null;
try {
if (options.data) {
jData = $.parseJSON(options.data);
if (jData.RedirectUrl) {
window.location = jData.RedirectUrl;
}
}
} catch (e) {
// not json
}
if (!jData && options.callback) {
options.callback(options.data);
}
};
$.ajax({
type: "GET",
url: "Myurl",
dataType: "html",
success: function (data) {
loadOrRedirect(
{
data: data,
callback: function (html) {
replaceRow.replaceWith(html);
alternateRowHighlighting();
}
});
}
});
可能重複[RedirectToRoute( 「默認」)和重定向(RETURNURL)正在返回 '200',而不是「302 「(http://stackoverflow.com/questions/2355151/redirecttoroutedefault-and-redirectreturnurl-are-returning-200-instead-o) - 關於這個問題的OP基本上是做你(AJAX負載或重定向)什麼 –