Monday, January 5, 2015

jQuery POST to ASP.NET MVC Controller

I recently created some code to us jQuery to post to a MVC Controller.

Here's the code.

I was a little surprised at the different return values from the C#.

In the case of a success, I am returning a JSON object to the client, which is being processed in the done callback of the jquery promise.

When there is an error, there is a response code with a message sent back to the client which is handled by the fail callback.

From my reading and searches on Stack Overflow, these appear to be the best practices as of this writing for return values from a HttpPost Action.

Note that I am not posting a form directly.  If you do that, your HttpPost service should also include a redirect like this:

    return RedirectToAction("Index", "Work");