The scenario is in an ASP.NET MVC (4,5) maxing an ajax call that returns a partial view. Sometimes however based on different situations I may need to return an error message - display something for the user for example..
My current approach is this.
in 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 }
}
});
In the controller:
public ActionResult ServerMethod(int id)
{
if (id = 0)
return new HttpStatusCodeResult(405);
if (id = 1)
return new HttpStatusCodeResult(406);
//otherwise..
return PartialView("View", Model);
}
I am however aware this is a hack and not a proper solution.. Is there any better way of doing this?