0
votes

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?

2

2 Answers

4
votes

You could return a JsonResult in your fail cases. You could add any properties you want to your Json.

Something like this:

public ActionResult ServerMethod(int id)
{
    if (id == 0)
    {
        return Json(new
        {
            Failed = true,
            FailType = "Type1", // Whatever makes sense to you
            //OtherRelevantProperty = whatever
        });
    }

    if (id == 1)
    {
        return Json(new
        {
            Failed = true,
            FailType = "Type2", // Whatever makes sense to you
            //OtherRelevantProperty = whatever
        });
    }

    //otherwise.. 
    return PartialView("View", Model);
}   

In your ajax call you could do this:

$.ajax({
    url: url,
    type: "POST",
    data:{ id: id},
    done: function (response) {
        if (response.Failed) {
            // Switch by response.FailType and use any relevant
            // json properties you created in the controller
        }
        else {
            $("#container").html(response);
        }
    }
});

If you absolutely need to also return a failed status code, you could set it before returning the json such as:

if (id == 0)
{
    Response.StatusCode = 500;

    return Json(new
    {
        Failed = true,
        FailType = "Type1", // Whatever makes sense to you
        //OtherRelevantProperty = whatever
    });
}

And then you could handle the failed case in your fail callback:

$.ajax({
    url: url,
    type: "POST",
    data:{ id: id},
    done: function (response) {
        $("#container").html(response);
    },
    fail: function(xhr) {
        var response = JSON.parse(xhr.responseText);
        if (response.Failed) {
            // Switch by response.FailType and use any relevant
            // json properties you created in the controller
        }
    }
});

Please note that I haven't tested this particular code and it is for demonstration only. I am, however, using a similar set-up in my projects.

1
votes

you can set

Response.StatusCode = 405;

and then return the view model

return PartialView("View", Model);

and then trace it in the error section?