Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 601
NathanD Avatar asked Oct 22 '08 21:10

NathanD


People also ask

Can we return partial view from action method?

To return a Partial view from the controller action method, we can write return type as PartialViewResult and return using PartialView method.

Can we return partial view in MVC?

In ASP.NET Core MVC, a controller's ViewResult is capable of returning either a view or a partial view. In Razor Pages, a PageModel can return a partial view represented as a PartialViewResult object. Referencing and rendering partial views is described in the Reference a partial view section.

What is return JSON in MVC?

What is JsonResult ? JsonResult is one of the type of MVC action result type which returns the data back to the view or the browser in the form of JSON (JavaScript Object notation format).

How display JSON data in HTML using Ajax in asp net?

Create target "JSON object Mapper" object class file according to the business requirements. Create a "Controllers\HomeController. cs" file with default Index method and GetData(...) method with string type input query parameters for Ajax call with following lines of code i.e.


2 Answers

In your action method, return Json(object) to return JSON to your page.

public 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"); 
like image 183
Haacked Avatar answered Oct 11 '22 08:10

Haacked


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 

Hope this helps James

like image 38
James Green Avatar answered Oct 11 '22 07:10

James Green