5

Whats the simplest way of just returning an int from an Ajax MVC Action call?

I am currently trying:

public ContentResult Create(MyModel model)
{
    return Content("1");
}

using (Ajax.BeginForm("Create",
        new AjaxOptions {
            OnComplete = "function(data) { alert(data); }"
        }))

I get alert [object Object]. How do I get the int value? Or if possible return the int directly instead of having to use a ContentResult?

fearofawhackplanet
  • 52,166
  • 53
  • 160
  • 253

1 Answers1

7

I would do something like this:

public JsonResult Create(MyModel model)
{
    return Json(new { Result = 1 });
}

using (Ajax.BeginForm("Create",
        new AjaxOptions {
            OnComplete = "function(data) { alert(data.get_response().get_object().Result); }"
        }))
Craig Stuntz
  • 125,891
  • 12
  • 252
  • 273