Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>When you do <code>return Json(...)</code> you are specifically telling MVC <em>not to use a view</em>, and to serve serialized JSON data. Your browser opens a download dialog because it doesn't know what to do with this data.</p> <p>If you instead want to return a view, just do <code>return View(...)</code> like you normally would:</p> <pre><code>var dictionary = listLocation.ToDictionary(x =&gt; x.label, x =&gt; x.value); return View(new { Values = listLocation }); </code></pre> <p>Then in your view, simply encode your data as JSON and assign it to a JavaScript variable:</p> <pre><code>&lt;script&gt; var values = @Html.Raw(Json.Encode(Model.Values)); &lt;/script&gt; </code></pre> <hr> <p><strong>EDIT</strong></p> <p>Here is a bit more complete sample. Since I don't have enough context from you, this sample will assume a controller <code>Foo</code>, an action <code>Bar</code>, and a view model <code>FooBarModel</code>. Additionally, the list of locations is hardcoded:</p> <p><strong>Controllers/FooController.cs</strong></p> <pre><code>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); } } </code></pre> <p><strong>Models/FooBarModel.cs</strong></p> <pre><code>public class FooBarModel { public IEnumerable&lt;SelectListItem&gt; Locations { get; set; } } </code></pre> <p><strong>Views/Foo/Bar.cshtml</strong></p> <pre><code>@model MyApp.Models.FooBarModel &lt;script&gt; var locations = @Html.Raw(Json.Encode(Model.Locations)); &lt;/script&gt; </code></pre> <p>By the looks of your error message, it seems like you are mixing incompatible types (i.e. <code>Ported_LI.Models.Locatio‌​n</code> and <code>MyApp.Models.Location</code>) 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, <code>new FooBarModel</code> in the controller matches <code>@model MyApp.Models.FooBarModel</code> in the view.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload