如何在Javascript中使用C#List ?(How to use C# List in Javascript? [duplicate])

编程入门 行业动态 更新时间:2024-10-22 23:01:09
如何在Javascript中使用C#List ?(How to use C# List in Javascript? [duplicate])

这个问题在这里已有答案:

如何使用Razor将未编码的Json写入我的View? 3个答案

我想在javascript中使用我的C#列表。 我试过这些,但它不起作用;

服务器端:

List<TimeLine> myList= new List<TimeLine>(); TimeLine element= new TimeLine (); element.Name= "abc"; element.About= "ssss"; element.Id = id; myList.Add(element); System.Web.Script.Serialization.JavaScriptSerializer oSerializer = new System.Web.Script.Serialization.JavaScriptSerializer(); model.JSONList= oSerializer.Serialize(myList); return View(model);

客户端:

$(function() { Draw(); }); function Draw() { var tunel = '@Model.JSONList'; var str = ""; if (tunel.length > 0) { for (var i = 0; i < tunel.length; i++) { str += '<span>'+tunel[i].Name+'</span>'+ '<span>'+tunel[i].Id+'</span>'+ '<span>'+tunel[i].About+'</span>'; } $("#myDiv").append(str); } }

你能帮我把列表中的对象拿到JavaScript中吗?

谢谢。

注意:我也尝试过JSON.Parse('@ Model.JSONList'); 但它不起作用; 这是我的详细价值。 var tunel = JSON.parse('[{&quot;Id&quot;:3,&quot;Tarih&quot;:&quot;\/Date(1425506400000)\/&quot;,&quot;Tip&quot;:2,&quot;Hakkinda&quot;:&quot;- Proje, g&#246;rev, todo d&#246;k&#252;mlerini g&#246;ster.&quot;,&quot;Adı&quot;:&quot;Profil Ekranının Tasarlanması&quot;},{&quot;Id&quot;:26,&quot;Tarih&quot;:&quot;\/Date(1425160800000)\/&quot;,&quot;Tip&quot;:1,&quot;Hakkinda&quot;:&quot;Kişisel işlerin d&#252;zenlenmesi i&#231;in geliştirilen proje.&quot;,&quot;Adı&quot;:&quot;Kişisel Yazılım Asistanı&quot;}]')

This question already has an answer here:

How do I write unencoded Json to my View using Razor? 3 answers

I want to use my C# list in javascript. I tried these, but it doesnt work;

Server side:

List<TimeLine> myList= new List<TimeLine>(); TimeLine element= new TimeLine (); element.Name= "abc"; element.About= "ssss"; element.Id = id; myList.Add(element); System.Web.Script.Serialization.JavaScriptSerializer oSerializer = new System.Web.Script.Serialization.JavaScriptSerializer(); model.JSONList= oSerializer.Serialize(myList); return View(model);

Client side:

$(function() { Draw(); }); function Draw() { var tunel = '@Model.JSONList'; var str = ""; if (tunel.length > 0) { for (var i = 0; i < tunel.length; i++) { str += '<span>'+tunel[i].Name+'</span>'+ '<span>'+tunel[i].Id+'</span>'+ '<span>'+tunel[i].About+'</span>'; } $("#myDiv").append(str); } }

Can you help me to get the object in list and use it in JavaScript?

Thanks.

Note: I also tried JSON.Parse('@Model.JSONList'); but it doesnt work; Here is my detailed value. var tunel = JSON.parse('[{&quot;Id&quot;:3,&quot;Tarih&quot;:&quot;\/Date(1425506400000)\/&quot;,&quot;Tip&quot;:2,&quot;Hakkinda&quot;:&quot;- Proje, g&#246;rev, todo d&#246;k&#252;mlerini g&#246;ster.&quot;,&quot;Adı&quot;:&quot;Profil Ekranının Tasarlanması&quot;},{&quot;Id&quot;:26,&quot;Tarih&quot;:&quot;\/Date(1425160800000)\/&quot;,&quot;Tip&quot;:1,&quot;Hakkinda&quot;:&quot;Kişisel işlerin d&#252;zenlenmesi i&#231;in geliştirilen proje.&quot;,&quot;Adı&quot;:&quot;Kişisel Yazılım Asistanı&quot;}]')

最满意答案

你需要解析你的JSON:

var tunel = JSON.parse('@Model.JSONList');

否则你只是对字符串进行所有操作。

如果你是字符串出来编码(例如&quot;而不是" ),那么你可以尝试使用@Html.Raw(Model.JSONList) :

var tunel = JSON.parse('@Html.Raw(Model.JSONList)');

You need to parse your JSON:

var tunel = JSON.parse('@Model.JSONList');

otherwise you're just doing all your operations on a string.

If you're string is coming out encoded (e.g. &quot; instead of ") then you can try using @Html.Raw(Model.JSONList):

var tunel = JSON.parse('@Html.Raw(Model.JSONList)');

更多推荐

本文发布于:2023-04-29 03:09:00,感谢您对本站的认可!
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:如何在   Javascript   duplicate   List

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!