Ccna final exam - java, php, javascript, ios, cshap all in one. This is a collaboratively edited question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.
Friday, May 18, 2012
ASP.NET MVC controller actions that return JSON or partial html
I am trying to create controller actions which will return either JSON or partial html depending upon a parameter. What is the best way to get the result returned to an MVC page asynchronously?
I think you should consider the AcceptTypes of the request. I am using it in my current project to return the correct content type as follows.
Your action on the controller can test it as on the request object
if (Request.AcceptTypes.Contains("text/html")) { return View(); } else if (Request.AcceptTypes.Contains("application/json")) { return Json( new { id=1, value="new" } ); } else if (Request.AcceptTypes.Contains("application/xml") || Request.AcceptTypes.Contains("text/xml")) { // }
You can then implement the aspx of the view to cater for the partial xhtml response case.
Then in jQuery you can fetch it passing the type parameter as json:
$.get(url, null, function(data, textStatus) { console.log('got %o with status %s', data, textStatus); }, "json"); // or xml, html, script, json, jsonp or text
To answer the other half of the question, you can call:
return PartialView("viewname");
when you want to return partial HTML. You'll just have to find some way to decide whether the request wants JSON or HTML, perhaps based on a URL part/parameter.
In your action method, return Json(object) to return JSON to your page.
ReplyDeletepublic ActionResult SomeActionMethod() {
return Json(new {foo="bar", baz="Blech"});
}
Then just call the action method using Ajax. You could use one of the helper methods from the ViewPage such as
<%= Ajax.ActionLink("SomeActionMethod", new AjaxOptions {OnSuccess="somemethod"}) %>
SomeMethod would be a javascript method that then evaluates the Json object returned.
If you want to return a plain string, you can just use the ContentResult:
public ActionResult SomeActionMethod() {
return Content("hello world!");
}
ContentResult by default returns a text/plain as its contentType.
This is overloadable so you can also do:
return Content("<xml>This is poorly formatted xml.</xml>", "text/xml");
NathanD,
ReplyDeleteI think you should consider the AcceptTypes of the request. I am using it in my current project to return the correct content type as follows.
Your action on the controller can test it as on the request object
if (Request.AcceptTypes.Contains("text/html")) {
return View();
}
else if (Request.AcceptTypes.Contains("application/json"))
{
return Json( new { id=1, value="new" } );
}
else if (Request.AcceptTypes.Contains("application/xml") ||
Request.AcceptTypes.Contains("text/xml"))
{
//
}
You can then implement the aspx of the view to cater for the partial xhtml response case.
Then in jQuery you can fetch it passing the type parameter as json:
$.get(url, null, function(data, textStatus) {
console.log('got %o with status %s', data, textStatus);
}, "json"); // or xml, html, script, json, jsonp or text
Hope this helps
James
Another nice way to deal with JSON data is using the JQuery getJSON function. You can call the
ReplyDeletepublic ActionResult SomeActionMethod(int id)
{
return Json(new {foo="bar", baz="Blech"});
}
method from the jquery getJSON method by simply...
$.getJSON("../SomeActionMethod", {
id: someId
},
function(data) {
alert(data.foo);
alert(data.baz);}
To answer the other half of the question, you can call:
ReplyDeletereturn PartialView("viewname");
when you want to return partial HTML. You'll just have to find some way to decide whether the request wants JSON or HTML, perhaps based on a URL part/parameter.
For folks who have upgraded to MVC 3 here is a neat way
ReplyDeleteUsing MVC3 and Json