Passing JSON data from controller action to a razor view?

Returning JSON to your razor view is probably not the best method. I would suggest use a viewModel which is a c# class by itself.

Up vote 0 down vote favorite share g+ share fb share tw.

I would like to know how can I pass data from controller to view in mvc3 razor. The view is in . Cshtml file.

I have a variable in the controller where I am storing the data I need to push to the listbox control in the view. Var results = data. Select(item => new { id = item, label = item.

Value, value = item. Key }); Doing this: return Json(results, JsonRequestBehavior. AllowGet); Gives me only a popup with the data which needs to be pushed to the listbox: In the view the listbox resides in accordion control: @{int i=0;} @foreach (var item in Model.

Parameters) { @Html. LabelFor(m => item. Name, item.

Prompt) i++; } So, I would like to know what should I do to push the items to the listbox instead of showing a popup for the control Beginner in MVC, Thanks for your understanding. Thanks in advance, Laziale EDIT: Json format output {System.Linq.Enumerable. WhereSelectListIterator,f__AnonymousType1,string,string>>} asp.net asp.net-mvc asp.net-mvc-3 razor link|improve this question edited Feb 20 at 13:13Baz1nga5,1591314 asked Feb 20 at 11:25Laziale56229 72% accept rate.

Could you please post the format the JSON is being given to your view by your controller. – Rory McCrossan Feb 20 at 11:29 @RoryMcCrossan please check the post is updated with the format. Thx – Laziale Feb 20 at 12:08.

Returning JSON to your razor view is probably not the best method. I would suggest use a viewModel which is a c# class by itself. Namespace Test { public class ViewModel { public bool GivingAPresentation { get; set; } } } public class MyController: Controller { public virtual ActionResult Index() { var model=new ViewModel(){GivingAPresentation =true;} return View(model); } } Your view code: @model Test.

ViewModel GivingAPresentation: @Model. GivingAPresentation If you are forced to work with a JSON object that is returned from your action then you need to deserialize it first and then work with that object. You can read this post http://www.drowningintechnicaldebt.com/ShawnWeisfeld/archive/2010/08/22/using-c-4.0-and-dynamic-to-parse-json.aspx on how to parse JSON to a c# dynamic object.

Let me know if that helps.

Thanks for your comment, but I need to incorporate that in existing control which is already fetching data, but in other way. Do you think it's possible to do that? Thx – Laziale Feb 20 at 12:41 I have added a link and updated my answer.. see if that helps.. – Baz1nga Feb 20 at 13:13.

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions