admin管理员组文章数量:1390775
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.
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.
Share Improve this question asked Apr 15, 2011 at 18:59 iLemmingiLemming 36.4k61 gold badges198 silver badges316 bronze badges 1-
What is the
T
? Is it serializable? – Jamie Treworgy Commented Apr 15, 2011 at 19:02
2 Answers
Reset to default 3You 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.
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
本文标签: jqueryHowTo pass ListltTgt object to Javascript in MVCStack Overflow
版权声明:本文标题:jquery - HowTo pass List<T> object to Javascript in MVC? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744753770a2623337.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论