【发布时间】:2017-03-08 17:31:14
【问题描述】:
该场景是在 ASP.NET MVC (4,5) 中最大化返回部分视图的 ajax 调用。但是,有时根据不同的情况,我可能需要返回错误消息 - 例如为用户显示一些内容..
我目前的做法是这样的。
在 JS 中:
$.ajax({
url: url,
type: "POST",
data:{ id: id},
success: function (response) {
$("#container").html(response);
},
error: function (er) {
if (er.status == "405")
{// do someth }
else if (er.status == "406")
{// do someth else }
}
});
在控制器中:
public ActionResult ServerMethod(int id)
{
if (id = 0)
return new HttpStatusCodeResult(405);
if (id = 1)
return new HttpStatusCodeResult(406);
//otherwise..
return PartialView("View", Model);
}
但是我知道这是一个 hack 而不是一个正确的解决方案。有没有更好的方法来做到这一点?
【问题讨论】:
标签: ajax asp.net-mvc error-handling http-error