Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Return int from MVC Action with Ajax.BeginForm

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?

like image 657
fearofawhackplanet Avatar asked Jul 02 '10 13:07

fearofawhackplanet


1 Answers

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); }"
        }))
like image 196
Craig Stuntz Avatar answered Oct 29 '22 05:10

Craig Stuntz