Return int from MVC Action with Ajax.BeginForm
Asked Answered
N

1

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?

Nosewheel answered 2/7, 2010 at 13:40 Comment(0)
J
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); }"
        }))
Janellajanelle answered 2/7, 2010 at 13:48 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.