I'm using ASP.NET MVC and I'm trying to generate a piece of javascript as part of the view rendering. I have a model that expose an array of simple types and I would like to generate a javascript/json equivalent array into the view so that I can act on it using jQuery. So given the following model:
public class Info {
public string Name {get;set;}
public int ID {get; set;}
}
public class InfoModel{
public Info[] InfoList {get;set;}
}
...I would like to generate a javascript array looking like this:
var infoList = [
{
Name = "...",
ID = 1
} ,
{
Name = "...",
ID = 2
},
....
{
Name = "...",
ID = N
}];
Is there a nice and concise way to do this in the view, I seem to have trouble with encoding of quotes if I try to have the model generate a json representation, so currently I can only have it generated using some spaghetti/classic asp code that I would rather have replaced by a nice one-liner.
EDIT: Note that I'm not looking for away to have a controller return a JsonResult, I want a way for my view to contain a javascript array that is generated from my model in a single line of code (if possible)
EDIT: I got part of the way, but seems to be struggling with encoding. This code in the view:
<script>
var list = <%: HtmlExtension.ToJson(Model.InfoList) %>;
</script>
(where ToJson encapsulates conversion to string using JavaScriptSerializer) outputs some encoding faults:
var info = [{"Name":"Low End","ID":1}];
..which is not what I was looking for. I could do this:
var info = <% Response.Write(HtmlExtension.ToJson(Model.InfoList)); %>;
which works, but doesn't look as shiny. Should I explicitly ignore encoding as shown (The output is sane, not user generated, so it may not be a problem) or am I missing something else that makes it less classic asp?
The Controller Action method will be called using jQuery POST function and JSON data will be returned back to the View using JsonResult class object. In this article I will explain with an example, how to use the JsonResult class object for returning JSON data from Controller to View in ASP.Net MVC.
Right click on Controller folder in the created MVC application, give the class name Home or as you wish and click OK. In the above controller class JsonResult method EmpDetails we have added the records into the Generic list and returning it as JSON to avoid database query for same result.
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).
Json(Object, String, Encoding, JsonRequestBehavior) Creates a JsonResult object that serializes the specified object to JavaScript Object Notation (JSON) format using the content type, content encoding, and the JSON request behavior.
It seems you have an encoding problem. I believe you have two choices here:
MvcHtmlString
instead of a normal string
, or...<%: ... %>
style to write out your code, use <%= ... %>
to not do encoding. If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With