Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ASP.NET MVC - Proper way to handle ajax actions with no return object

I have a controller action that does some work in the database and then exits when it's finished. This action is being called via jQuery's ajax function with the dataType set to 'json'.

If I set the return type of the action to void, everything will function just fine except Firefox will show an error in the console that says: "no element found".

It makes sense that Firefox would throw this error if it was expecting XML to come back. However, even when I change the dataType property of the ajax call to "text", I still receive the error. In order to get rid of the error with the return type void, I would have to set the Response's ContentType to "text/html". Or I could set the return type to JsonResult and return a new [empty] JsonResult object.

I'm sure there are several ways I can make this error go away, but I wanted to know the proper way to handle actions with no return values being called via ajax.

If it matters, I'm also using the async controller action pattern.

public void DoSomethingAsync(SomeJsonObjectForModelBinding model)
{
    // do some database things
}

public void DoSomethingCompleted()
{
    // nothing to do...
    // what should my return type be?
    // do I need to set the content type here?
}
like image 1000
harryfino Avatar asked Mar 28 '11 18:03

harryfino


People also ask

How is Ajax implemented in MVC?

The MVC Framework contains built-in support for unobtrusive Ajax. You can use the helper methods to define your Ajax features without adding a code throughout all the views. This feature in MVC is based on the jQuery features. To enable the unobtrusive AJAX support in the MVC application, open the Web.

What is unobtrusive Ajax in MVC?

The idea behind Unobtrusive AJAX is that AJAX behaviour is attached to form and anchor elements via HTML5 data-* attributes, instead of binding click event handlers in script blocks. In old MVC, these attributes can be generated from Html helpers: Ajax. BeginForm and Ajax. ActionLink and then setting some AjaxOptions .

How do you call Ajax action in method?

ajax({ url: "Customer/InsertCustomer", data: { 'ContactID': ContactID, 'Company': Company, 'Status': Status, 'IsActive': IsActive, 'Comments': Comments, 'Country': Country, 'Address1': Address1, 'Address2': Address2, 'City': City, 'State': State, 'PostalCode': PostalCode, 'VatNo': VatNo, 'RegNo': RegNo, 'Phone': Phone, ...


2 Answers

I know this doesn't exactly answer your question, but I would argue that you should always have a return value coming back from an AJAX or web service call. Even if only to tell you that the operation was successful, or otherwise return the error (message) back to you.

I often define a class like this:

public class JsonResultData
{
    private bool _success = true;
    public bool Success
    {
        get { return _success; }
        set { _success = value; }
    }

    public object Value { get; set; }
    public List<string> Errors { get; set; }


    public JsonResultData()
    {
        this.Errors = new List<string>();
    }
}

And then use it to return data or any other call meta data in the JsonResultData wrapper like so:

return new JsonResult {
            Data = new JsonResultData { Value = returnValue, Success = true }
           };
like image 106
Kon Avatar answered Nov 11 '22 14:11

Kon


I can't comment because of my reputation but I still wanted to contribute to clear the confusion in Kon's answer.

In an application I caught all exceptions within an ActionMethod, set an HttpStatusCode and added an error message to the response. I extracted the message in the Ajax error function and showed it to the user.

Everything worked out fine until the application got put on the staging server, who had some kind of settings that did not allow a return message within an erroneous response. Instead some standard Html was transmitted resulting in a JS error processing the response.

In the end I had to rewrite all my exception handling returning my application errors as successful Ajax call (which it actually is) and then differ within the Ajax success function, just the way it should be.

You should not mix system-level and application-level feedback. You may not be able to control the system-level feedback the way your application needs.

like image 41
Otto Abnormalverbraucher Avatar answered Nov 11 '22 16:11

Otto Abnormalverbraucher