我正在使用ASP.NET MVC与jQuery。我有以下MVC Action返回成功的部分页面。在应用程序错误,我不知道发送它正确处理它在客户端:
public ActionResult LoadFilterSet(int filterSetId)
{
try
{
BreadCrumbManager bcManager = this.ResetBreadCrumbManager(this.BreadCrumbManagerID);
GeneralHelper.LoadBreadCrumbManager(bcManager,filterSetId);
ViewData["BreadCrumbManager"] = bcManager;
return View("LoadFilterSet");
}
catch (Exception ex)
{
return Content("");
}
}
以下是我的jQuery ajax调用。请注意,我正在检查数据长度以确保没有错误。请建议我一个更好的方法来做到这一点。
$.ajax({
type: "GET",dataType: "html",async: true,data: ({ filterSetId: selectedId }),url: link,contentType: "text/html; charset=utf-8",success: function(data,textStatus) {
if (data.length > 0) {
// Clear the local filters first.
clearLocalFilters();
$('td.selected-filters table.filters-display').append(data);
}
}
});
解决方法
我将在您设置的ajax调用中添加一个错误函数。让服务器确定显示错误信息并传递给它ajax错误处理程序并让它显示。
success: function(data,textStatus) {
// Clear the local filters first.
clearLocalFilters();
$('td.selected-filters table.filters-display').append(data);
},error: function (data) {
alert(data.responseText); // use any display logic here
}
在控制器的操作中,如果发现错误
Response.StatusCode = (int)HttpStatusCode.BadRequest; return Content(errorMessage,MediaTypeNames.Text.Plain);