1

I want to return a JSON result. To do this I have a controller method as follows that is called from a Ajax.BeginForm on the View:

@using (Ajax.BeginForm("Update",  new AjaxOptions { OnSuccess = "MySuccessMethod()" }))
{
    <!-- some form stuff -->
    <input type="submit" value="Submit"/>
} 

This is the controller that handles it:

[HttpPost]
public JsonResult Update(FormCollection fc)
{
    // Process form stuff
    return Json (new {success = true });
}

What I want is to process the success response with MySuccessMethod. What I see is that the view on submit goes to the correct controller method above, which then redirects the page to the URL /Home/Update with the following string in the screen:

{"success": true }

Not sure if it is relevant but I am using Mono.

How can I make the framework not switch pages to /Home/Update nor display the JSON string on the view and just process the JSON in the back?

user2609980
  • 10,264
  • 15
  • 74
  • 143

1 Answers1

3

For your first question, check the following:

1) Make sure you have Microsoft.jQuery.Unobtrusive.Ajax included and referenced

2) OnSuccess = "MySuccessMethod()" should be OnSuccess = "MySuccessMethod" (where MySuccessMethod is a JavaScript method, not a C# one)

For your second question, you could have your method return ActionResult instead of JsonResult (see here for more information). JsonResult is a type of ActionResult, which means that updating your action to return ActionResult will allow your method to return multiple types of ActionResult depending on the scenario:

[HttpPost]
public ActionResult SomeThing(int randomParam)
{
    if (randomParam == 0)
    { 
        return Json("Zero!"); 
    }
    else if (randomParam == 1)
    {
        return View("Not zero!");
    }
    else
    {
        return HttpNotFound("Error: I can only find zeroes and ones");
    }
}

As a general rule of thumb (although sometimes rules are meant to be broken), having your action return one type (like your example, JsonResult instead of ActionResult) makes your action less error-prone as, for example, Visual Studio will let you know if you accidentally try to return another type of result - use ActionResult when your action returns more than one type of result.

trashr0x
  • 6,457
  • 2
  • 29
  • 39
  • Thanks. Is it possible to post a viewmodel to a controller using an ajax form? – user2609980 Dec 12 '14 at 16:34
  • 1
    Absolutely. You'll just have to make sure that the object you are passing to your controller via the form matches your viewmodel. An example can be found [here](http://stackoverflow.com/questions/9871365/mvc3-passing-viewmodel-to-controller-method-using-jquery-ajax). – trashr0x Dec 12 '14 at 17:39
  • Yes thanks! Since the viewmodel has a two objects with lots of properties I decided to only use the primary key and the one value I am updating. Then I loop through a formcollection and update the database. Is this a good decision or is there a better way? – user2609980 Dec 12 '14 at 17:46
  • 1
    There's no need to send each property of the viewmodel over to the controller if you're only updating one value, so your decision is valid. – trashr0x Dec 12 '14 at 17:57