Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Generate raw json array into view in ASP.NET MVC

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 = [{&quot;Name&quot;:&quot;Low End&quot;,&quot;ID&quot;: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?

like image 694
soren.enemaerke Avatar asked Nov 03 '10 08:11

soren.enemaerke


People also ask

How do I return JSON data to view?

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.

How show JSON data table in HTML 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.

Can ActionResult return JSON?

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).

What is JsonResult MVC?

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.


1 Answers

It seems you have an encoding problem. I believe you have two choices here:

  1. When you create your extension, be sure that it returns MvcHtmlString instead of a normal string, or...
  2. Instead of using the <%: ... %> style to write out your code, use <%= ... %> to not do encoding.
like image 179
Jonathan Bates Avatar answered Sep 19 '22 08:09

Jonathan Bates