System.Collections.Generic.List从CS到变量的js [英] System.Collections.Generic.List from CS to Js variable
本文介绍了System.Collections.Generic.List从CS到变量的js的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
在MyPage.aspx.cs我有对象的列表
on MyPage.aspx.cs I have List of object
protected List<MyObj> myObjList =null;
protected void Page_Load(object sender, EventArgs e)
{
myObjList = GetObjByUserId("23423");
}
aspx页面上我想的对象这个列表分配给JS变量
on aspx page I want to assign this list of objects to JS variable
<script type="text/javascript">
$(document).ready(function () {
var BookingsList = <%=myObjList %>;
</script>
但分配类型如String =>
but is assign type like string=>
System.Collections.Generic.List`1[myObj]
我怎么能到我的对象集合从CS分配给JS变量?
how I can to assign my collection of object from CS to JS variable?
推荐答案
尝试使用的JavaScriptSerializer类似以下内容:
Try using JavaScriptSerializer like following:
C#code
Student student = new Student();
System.Web.Script.Serialization.JavaScriptSerializer oSerializer = new System.Web.Script.Serialization.JavaScriptSerializer();
string StudentJson = oSerializer.Serialize(resources);
和在你的aspx code把它想:
and in your aspx code get it like:
<script type="text/javascript">
var jsonStudent = <%=StudentJson%>;
</script>
请确保StudentJson在后端类中的公共或受保护的财产
Please make sure that StudentJson is a public or protected property in your backend class
这篇关于System.Collections.Generic.List从CS到变量的js的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文