Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to return Json object from MVC controller to view

I am doing an MVC application where i need to pass json object from controller to view.

var dictionary = listLocation.ToDictionary(x => x.label, x => x.value); return Json(new { values = listLocation}, JsonRequestBehavior.AllowGet); 

The above code i am using in my controller , now when i deploy the view page its opening a download dialog in my browser , when open the file it gives me json object as i needed format.

Now i want to return my view page also want to access the json object in the view page. how can i do that.

like image 642
Purushoth Avatar asked Mar 04 '13 07:03

Purushoth


People also ask

How can I get JSON result in MVC?

Right click on Home folder inside the View folder in the created MVC application as in the following screenshot: Give the name EmpDetails and click Add button. To bind view using json we need JQuery and the following JQuery library to communicate to the controller from view: <script src="~/Scripts/jquery-1.10.

What is return JSON in MVC?

JsonResult is an ActionResult type in MVC. It helps to send the content in JavaScript Object Notation (JSON) format.


2 Answers

When you do return Json(...) you are specifically telling MVC not to use a view, and to serve serialized JSON data. Your browser opens a download dialog because it doesn't know what to do with this data.

If you instead want to return a view, just do return View(...) like you normally would:

var dictionary = listLocation.ToDictionary(x => x.label, x => x.value); return View(new { Values = listLocation }); 

Then in your view, simply encode your data as JSON and assign it to a JavaScript variable:

<script>     var values = @Html.Raw(Json.Encode(Model.Values)); </script> 

EDIT

Here is a bit more complete sample. Since I don't have enough context from you, this sample will assume a controller Foo, an action Bar, and a view model FooBarModel. Additionally, the list of locations is hardcoded:

Controllers/FooController.cs

public class FooController : Controller {     public ActionResult Bar()     {         var locations = new[]         {             new SelectListItem { Value = "US", Text = "United States" },             new SelectListItem { Value = "CA", Text = "Canada" },             new SelectListItem { Value = "MX", Text = "Mexico" },         };          var model = new FooBarModel         {             Locations = locations,         };          return View(model);     } } 

Models/FooBarModel.cs

public class FooBarModel {     public IEnumerable<SelectListItem> Locations { get; set; } } 

Views/Foo/Bar.cshtml

@model MyApp.Models.FooBarModel  <script>     var locations = @Html.Raw(Json.Encode(Model.Locations)); </script> 

By the looks of your error message, it seems like you are mixing incompatible types (i.e. Ported_LI.Models.Locatio‌​n and MyApp.Models.Location) so, to recap, make sure the type sent from the controller action side match what is received from the view. For this sample in particular, new FooBarModel in the controller matches @model MyApp.Models.FooBarModel in the view.

like image 129
Daniel Liuzzi Avatar answered Sep 19 '22 15:09

Daniel Liuzzi


You could use AJAX to call this controller action. For example if you are using jQuery you might use the $.ajax() method:

<script type="text/javascript">     $.ajax({         url: '@Url.Action("NameOfYourAction")',         type: 'GET',         cache: false,         success: function(result) {             // you could use the result.values dictionary here         }     }); </script> 
like image 27
Darin Dimitrov Avatar answered Sep 18 '22 15:09

Darin Dimitrov