3

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

3 Answers 3

13

I know this is an old question, but the answers are outdated, as it is now possible.

This can be done in the razor syntax using @: on the start of the lines to run in javascript (escape from c#).

My Model.xValues is a List<String> passed from the controller. and yValues is List<int>

Here is a quick example of how to loop through a model data inside a javascript function.

<script type="text/javascript">
   function drawChart() {

        // Create the data table.
        var data = new google.visualization.DataTable();
        data.addColumn('string', '@Model.LabelName');
        data.addColumn('number', '@Model.ValueName');

       @for(int i = 0; i < Model.xValues.Count; i ++)
       {
           @: data.addRow(['@Model.xValues.ElementAt(i)', @Model.yValues.ElementAt(i)]);
       }

       ... etc

</script>
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you for the very elegant solution. I was even able to do something like this: gist.github.com/saad749/db606466725cff8f96b5a766da4a4dad
3

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 %>;

1 Comment

Your advise is cool but I see question has 'in javascript' section, So I think that is better to change question title.
2

You will need to loop through your Model object in a .NET code block, and assign those values to your JavaScript variables in that loop:

var count = 0, yourobject; // This is JavaScript

<% for (var i = 0; i < Model.Inserts.Count(); i++) { %> // This is .NET 
       yourobject = '<%= Model.Inserts[i] %>'; // This is JS AND .NET
       mlb.add(yourobject, count++); // This is JavaScript
<% } %>

1 Comment

Wouldn't this output multiple var yourobject... statements? That would be a javascript error. I think you'd need to declare it outside the loop as well and simply reuse it inside the loop.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.