Isley Isley - 1 year ago 43
JSON Question

MVC controller returns JSON. But to where?

i'm new to MVC. So a MVC controller mainly returns a viewresult. i understand it will be the view has the same action name.
Let's say "DoSomething" action goes to "DoSomething" view. But if the controller returns a JSON, to where is it returned to?
Is it going to the page where the action method was activated from?
Let's say "Index" vew to "DoSomething" action. If "DoSomething" controller returns a JSON, does the JSON go to "Index" view?
I was working out with returning a JSON and JQuery call ajax, but am confused.
Thanks.

Answer Source

If an Action Method returns Json, the View doesn't get involved. In all cases, an action method of a controller returns something to response stream. Notice the return statement below. Actually it's calling the view first. Think of it like a method call. And then whatever the view is, is returned.

public ActionResult DoSomething()
{
     return View();
}

In Json's case notice the view stays out of it:

public JsonResult DoSomething()
{
     var jsondata = {
     "name"    : "Mike",
     "address" : "home",
     "gender"  : "male"
    };
     return Json(jsondata, JsonRequestBehavior.AllowGet);
}

jsondata will be caught by javascript.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download