将IEnumerable列表传递给javascript [英] Pass IEnumerable List to javascript

查看:57
本文介绍了将IEnumerable列表传递给javascript的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道是否可以在页面加载时将IEnumerable Collection传递给Javascript方法.所以像这样...

I was wondering if I could pass a IEnumerable Collection into a Javascript method on the page load. So something like this...

<%@ Control Language="C#" Inherits="System.Web.Mvc.ViewUserControl<MyAppMVC.Models.ViewModels.News.NewsIndexViewData>" %>

<div id="container">

    <%= String.Format("<script type='text/javascript'>testMethod({0})</script>", Model.NewsList)  %>

</div>

我意识到JS是客户端,只是不知道有没有办法做到这一点? 谢谢!

I realize JS is client side, just didn't know if there was any way possible to do that? Thanks!

推荐答案

否,您不能.有几个原因.

No, you cannot. For several reasons.

1)JavaScript中没有IEnumerable,因为您在.NET中使用它.有一些相似之处,但实现方式完全不同.在.NET中,IEnumerable仅表示该类提供了一种方法GetEnumerator(),该方法返回一个IEnumerator(其本身仅包含Current,MoveNExt和Reset方法).在JavaScript中,对项目执行for迭代时,您正在遍历其属性的名称.

1) There is no IEnumerable in JavaScript, as you use it in .NET. There is something similar, but it is implemented completely differently. In .NET, IEnumerable just means the class provides a method, GetEnumerator(), which returns an IEnumerator (which itself only contains Current, MoveNExt, and Reset methods). In JavaScript, when you do a for iteration on an item, you are iterating over the names of its properties.

var myObj = { 'a' = 1, 'b' = 2 };

for (var name in myObj) { alert(name); } // will alert 'a', and 'b'

即使使用JavaScript数组,上述循环也会返回数组元素的索引,而不是该索引处的实际成员.

Even when working with JavaScript arrays, the above loop returns the index of the array element, not the actual member at that index.

2)通过在列表上执行String.Format(),您不会一直将列表作为对象传递给JavaScript,而只是将列表的ToString()结果传递给了JavaScript.可能只返回"System.Collections.Generic.List`1 [System.String]"

2) By doing a String.Format() on your list, you wouldn't have been passing the list as an object to your JavaScript but just the ToString() result of your list. Which probably just returns "System.Collections.Generic.List`1[System.String]"

3)除非您的开发环境明确允许,否则您可以假定将参数从一种语言传递到另一种语言是行不通的.就像您无法在.NET代码中编写JavaScript一样,您也无法在JavaScript中编写.NET代码.这些语言具有不同的功能集,不同的语法,并以完全不同的机制执行-编译.NET,并解释JavaScript(通常来说)(

3) Unless your developing environment explicitly allows it, you can assume that passing arguments from one language into another is not going to work. Much like you can't write JavaScript inside your .NET code, you can't write .NET code in your JavaScript. These languages have different feature sets, different syntax, and are executed with completely different mechanisms - .NET is compiled, and JavaScript (generally, speaking) is interpreted (Compiled vs. Interpreted languages).

您要做的就是将数据转换为JavaScript可以使用的格式.这很可能意味着将其转换为名为 JSON 的内容.您没有提供有关Model.NewList到底是什么,或者testMethod()作为参数的期望的详细信息.但是为了举例,我们假设NewList是一个字符串列表.在这种情况下,您的JSON将如下所示:

What you have to do is transform your data into a format that can be used by JavaScript. Most likely this means converting it into something called JSON. You didn't provide a lot of details as to what exactly Model.NewList is, or what your testMethod() expects as an argument. But for the sake of an example lets assume NewList is a list of strings. In that case, your JSON would look something like this:

{ 'NewList' : ['string1', 'string2', 'string3'] }

将.NET数据转换为JSON的最简单方法是使用内置库,例如JavaScriptSerializer:

The easiest way to convert your .NET data into JSON is to use built-in libraries, such as JavaScriptSerializer:

System.Web.Script.Serialization.JavaScriptSerializer serializer = new System.Web.Script.Serialization.JavaScriptSerializer();
string json = serializer.Serialize(Mdoel.NewList);

这篇关于将IEnumerable列表传递给javascript的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆