需要如何填充从可视数据列表JavaScript数组简单的例子 [英] Need simple example of how to populate javascript array from Viewdata list

查看:113
本文介绍了需要如何填充从可视数据列表JavaScript数组简单的例子的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有可能是这样做的一个简单的方法,我所有的耳朵,如果有。我的情况是我有我成功地与文本和值填充表单上一个DropDownList。我还需要从数据库表所以当用户从相关数据获取表单上的文本框填充下拉菜单选择客户机上使用相同的表行有其他相关的字符串值。只有4条我处理使存储在客户端上没有什么大不了的。我想到了通过ViewData的传递这些数据作为一个列表,并加载到JavaScript数组。当用户从下拉选择 - 我将确定选择的索引,并获得相关的数据I从阵列需要。我已经使用了其他需要的数据下拉项的值,所以我需要一种方式来获得该相关数据不作回程到服务器。如果我在正确的轨道上可能有人张贴填充与返回作为Vi​​ewData的列表刺痛值的JS数组的一个简单的例子。

谢谢,

迈克


解决方案

  myarray的无功= [
<%的foreach(在计算机[名单]作为列表与LT字符串项;串>){%GT;
<%=项%GT;,
<%}%GT;
];

其一端一个逗号IE浏览器中会报打破,所以我建议一个视图扩展helper方法使code易于管理:

 <%= Html.JavaScriptArray(计算机[名单]作为名单<串>中myArray的)%>

将这个辅助方法,在某处你的解决方案:

 公共静态字符串JavaScriptArray(此的HtmlHelper的HtmlHelper,IList的<串GT;值,字符串的varName){
    StringBuilder的SB =新的StringBuilder(VAR);
    sb.append(varName的);
    sb.append(= [);
    的for(int i = 0; I< values​​.Count;我++){
        sb.append(');
        sb.append(值[I]);
        sb.append(');
        sb.append(我== values​​.Count - 1\\ n:\\ n); //不是prettiest但它的作品...
    }
    sb.append(];);
    返回sb.toString();
}

从技术上扩展方法可以去任何地方,你只需要包括命名空间,你.aspx文件。实际上最大努力让他们在逻辑上分开类,如 MyApp.Mvc.Extensions.JavaScriptExtensions MyApp.Mvc.Extensions.LinkExtensions

There may be a simpler way of doing this and I am all ears if there is. My situation is I have a dropdownlist on a form which I successfully populate with text and values. I also need to have additional related string values from the same table row in the db table available on the client so when the user selects from the dropdown this related data gets populated in a textbox on the form. There are only 4 records I'm dealing with so storing on the client is no big deal. I thought about passing this data via ViewData as a list and loading into a javascript array. When the user selects from the dropdown - I would determine the selected index and get the related data I need from the array. I am already using the value of the dropdown item for other required data so I need a way to get this related data without making a return trip to the server. If I am on the right track could someone post a simple example of populating a js array with sting values returned as a List in the ViewData.

Thanks,

Mike

解决方案

var myArray = [
<% foreach (string item in ViewData["list"] as List<string>) { %>
"<%= item %>",
<% } %>
];

Having a comma at the end will reportedly break in IE, so I would suggest a view extension helper method to make the code easier to manage:

<%= Html.JavaScriptArray(ViewData["list"] as List<string>, "myArray") %>

Put this helper method somewhere in you solution:

public static string JavaScriptArray(this HtmlHelper htmlHelper, IList<string> values, string varName) {
    StringBuilder sb = new StringBuilder("var ");
    sb.append(varName);
    sb.append(" = [");
    for (int i = 0; i < values.Count; i++) {
        sb.append("'");
        sb.append(values[i]);
        sb.append("'");
        sb.append(i == values.Count - 1 ? "\n" : ",\n"); // Not the prettiest but it works...
    }
    sb.append("];");
    return sb.toString();
}

Technically the extension method can go anywhere, you'll just need to include the namespace in you .aspx file. Practically its best to keep them in logically separated classes, such as MyApp.Mvc.Extensions.JavaScriptExtensions, MyApp.Mvc.Extensions.LinkExtensions

这篇关于需要如何填充从可视数据列表JavaScript数组简单的例子的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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