开发者

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 开发者_如何学Gohave 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?


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.


Yes there is a nice way:

InfoModel viewModel; // get this from wherever
return Json(viewModel);

MVC will translate the collection to the relevant JSON.

This will return a JsonResult (ie raw JSON).

Is that what you're after?

If you wanted to do something like this in your View:

<script type="text/javascript>
   var json = <%: Model.JsonStuff %>
   // more js
</script>

Then above example won't work.

You'll have to serialize it manually when you construct your ViewModel.

EDIT

Here's an example of how you could serialize your data.

JavaScriptSerializer js = new JavaScriptSerializer();
var listOfInfos; // this will be a collection (e.g List) of Infos.
var viewModel = new InfoModel
                {
                   InfoList = js.Serialize(listOfInfos);
                };
return View(viewModel);

As long as listOfInfo's implements IEnumerable, it will serialize to a JSON array.

You might have to play around with your viewmodel a bit, but that should put you on the right track.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜