3

I wanna do something like that:

 $(document).ready(function () 
{
    calendarGrid.create(@Model.Events)
}

Model.Events is a List.

I tried to use:

  • System.Web.Script.Serialization.JavaScriptSerializer.Serialize(@Model.Events)
  • JSON.parse(@Model.Events)
  • JSON.strigngify(@Model.Events)

nothing helps.

1
  • What is the T? Is it serializable? Commented Apr 15, 2011 at 19:02

2 Answers 2

3

You need to write code that will serialize your server-side list into code that gets sent to the client. Trye something like this:

calendarGrid.Create(@Html.Raw(JavaScriptSerializer.Serialize(Model.Events)))

The entire contents of @Html.Raw(...) will be emitted to the output.

2
  • 3
    Actually that should be calendarGrid.Create(@Html.Raw(JavaScriptSerializer.Serialize(Model.Events))) as the @ method HTML encodes and the output of the JavaScriptSerializer shouldn't be encoded in this case as it already does this. Commented Apr 15, 2011 at 19:12
  • it works. What's the right way to add DateTime conversion if my <T> has DateTime properties?
    – iLemming
    Commented Apr 15, 2011 at 19:19
2

I've had great success by setting a javascript variable to it, using:

<script>
     var eventList = @(Html.Raw(Json.Encode(Model.Events)));

     $(document).ready(function () {
        calendarGrid.create(eventList);
     });

</script>

From there, you can freely use the eventList variable as a JSON object.

The Trick is the use of Html.Raw to prevent any further encoding from happening

0

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.