2011-04-14 106 views
0

後,我有這樣的:

  • HTML:表單+ jQuery的
  • 控制器

我通過後jQuery的形式,在控制器中,有一個錯誤在我使用的函數中,我將狀態碼設置爲500.我想在$ .post的「.error」中顯示我的視圖的內容。正如你看到的我測試了警報,但我從來沒有看到的視圖的內容

HTML

@using (Html.BeginForm("Save","Customer")) 
{ 
} 

var jqxhr = $.post("Customer/Save", function() { 
    alert("success"); 
}) 
.success(function() {}) 
.error(function (jqXHR, status, error) { 
    alert(jqXHR); 
    alert(status), 
    alert(error); 
}) 
.complete(function() {}); 

* 控制器*

public ActionResult Save(MyModel model) 
{ 
    //Action1 here 
    //.... 

    if(Actiion1 failed) 
    { 
     Response.StatusCode = 500 
     return PartialView("Error"); 
    }  
    return View("MyView"); 
} 

感謝,

+0

你不需要添加錯誤部分查看和設置Response.StatusCode = 500;你只需要在錯誤函數中得到返回數據的responseText屬性,就像在我的文章中一樣 – AEMLoviji 2011-04-14 06:20:33

回答

2

你想被檢查jqXHRresponseText成員。

0
did you check if your async post request is coming into controller? If yes then instead of sending the partial view back, try to send some dummy string and check. 

希望這有助於。

艾比

0

我已經這樣做了。在頁面上我已經包含了這段代碼。

$(function() { 
    $.ajaxSetup({ 
     error: function (request, err) { 
      //   if (x.status == 0) { 
      //    alert('you are offline!!\n Check you network.'); 
      //   } else 
      if (request.status == 404) { 
       alert('page not found'); 
      } else if (request.status == 500) { 
       $('body').html(request.responseText); 
      } else if (err == 'parsererror') { 
       alert('parse error'); 
      } else if (err == 'timeout') { 
       alert('timeout'); 
      } else { 
       alert('unhandled exception.\n' + x.responseText); 
      } 
     } 
    }); 
    }); 

,你可以看到,我定身html屬性導致文本時error.status爲500它將設置錯誤頁面內容身體