asp.net mvc的 - 你怎么通过JavaScript的模型数据环 [英] asp.net mvc - how can you loop through model data in javascript

查看:118
本文介绍了asp.net mvc的 - 你怎么通过JavaScript的模型数据环的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我特林数据添加到在JavaScript中一个列表框,但字符串建筑语法我难住了:

VAR yourobject ='<%= Model.Inserts ['+ I +']%GT;';

导致错误:字符字符太多字符

所有code的:

  VAR MLB = cm.createListBox('mylistbox',{
                        标题:我的列表框,
                        ONSELECT:功能(ⅴ){
                                tinyMCE.activeEditor.windowManager.alert('价值选择:'+ V);
                            }
                        });
                        对于(VAR I = 0; I<'<%= Model.Inserts.Count()%GT;';我++){
                            VAR yourobject ='<%= Model.Inserts ['+ I +']%GT;';
                            mlb.add(yourobject,I);
                        }


解决方案

您不能混合在服务器上运行(即&LT的音译code; %%> 块),在这样的客户端(for循环)上的运行。难道在C#中的迭代,而不是JavaScript和在一个字符串,你可以简单地分配创建一个JavaScript对象。

 <%VAR阿雷=[;
    的foreach(在Model.Inserts变种插入){
        阿雷+ ='+插入+';
    }
    阿雷= aray.TrimEnd(,)+];
 %GT;
    VAR MLB = LT;%阿雷%取代;

I am tring to add data to a listbox in javascript, however the string building syntax has me stumped:

var yourobject = '<%=Model.Inserts['+ i + ']%>';

causes an error: "Too many characters in character literal"

All of the code:

var mlb = cm.createListBox('mylistbox', {
                        title: 'My list box',
                        onselect: function(v) {
                                tinyMCE.activeEditor.windowManager.alert('Value selected:' + v);
                            }
                        });


                        for (var i = 0; i < '<%=Model.Inserts.Count() %>'; i++) {
                            var yourobject = '<%=Model.Inserts['+ i + ']%>';
                            mlb.add(yourobject, i);
                        }

解决方案

You can't mix code that runs on the server (the transliteration of the <% %> block) with that the runs on the client (the for loop) in that way. Do the iteration in C# rather than in javascript and create a javascript object in a string that you can simply assign.

<%  var aray = "[";
    foreach (var insert in Model.Inserts) {
        aray += "'" + insert + "',";
    }
    aray = aray.TrimEnd(",") + "]";
 %>
    var mlb = <% aray %>;

这篇关于asp.net mvc的 - 你怎么通过JavaScript的模型数据环的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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