views:

32

answers:

1

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?

+1  A: 

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