需要如何从 Viewdata 列表填充 javascript 数组的简单示例

发布于 2024-07-15 12:01:54 字数 403 浏览 6 评论 0原文

可能有一种更简单的方法可以做到这一点,如果有的话我会洗耳恭听。 我的情况是我在表单上有一个下拉列表,我成功地用文本和值填充了该列表。 我还需要在客户端上可用的数据库表中的同一表行中提供其他相关字符串值,以便当用户从下拉列表中选择时,相关数据将填充在表单上的文本框中。 我只处理 4 条记录,因此存储在客户端上没什么大不了的。 我考虑过通过 ViewData 将这些数据作为列表传递并加载到 JavaScript 数组中。 当用户从下拉列表中选择时 - 我将确定所选索引并从数组中获取我需要的相关数据。 我已经将下拉项的值用于其他所需数据,因此我需要一种方法来获取这些相关数据,而无需返回服务器。 如果我走在正确的轨道上,有人可以发布一个简单的示例,使用 ViewData 中作为列表返回的字符串值填充 js 数组。

谢谢,

迈克

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

断肠人 2024-07-22 12:01:54
var myArray = [
<% foreach (string item in ViewData["list"] as List<string>) { %>
"<%= item %>",
<% } %>
];

据报道,末尾有逗号会在 IE 中中断,因此我建议使用视图扩展帮助程序方法来使代码更易于管理:

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

将此帮助程序方法放在解决方案中的某个位置:

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();
}

从技术上讲,扩展方法可以去任何地方,您只需需要在 .aspx 文件中包含命名空间。 实际上,最好将它们保留在逻辑上独立的类中,例如 MyApp.Mvc.Extensions.JavaScriptExtensions、MyApp.Mvc.Extensions.LinkExtensions

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

亢潮 2024-07-22 12:01:54

怎么样...

<%= new System.Web.Script.Serialization.JavaScriptSerializer().Serialize(ViewData["list"]) %>

how about...

<%= new System.Web.Script.Serialization.JavaScriptSerializer().Serialize(ViewData["list"]) %>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文