1

I am trying to use C# in javascript like we are using it in MVC Razor View using @ sign,

like suppose an array name list is passed to the View so we can access it in View like:

View

     Length of array :  <input type="text"  value="@Model.list.Length" />

                  Or we can iterate list array also like:

         @for(int i=0; i< Model.list.Length; i++)
         {
             console.log(Model.list[i]);
         }

But my question is how we can iterate or use this array in the javascript code , something similar to :

JS

      for(var i=0; i<@Model.list.Length; i++)
      {
        $("body").append("<h1></h1>").html(@Model.list[i]);
      }

Thanks !

4
  • 1
    You (usually) can't run C# on the client side, where Javascript usually runs. Commented Jun 4, 2012 at 13:09
  • This is like riding a bike inside a car. Both are vehicles and there are separate roads for them. C# runs on the server. JS runs inside the browser. Commented Jun 4, 2012 at 13:14
  • To make this work, you need a preprocessor, who translates the C# commands into native js. This is pretty easy for plain values, and a lot trickier for complex objects. If your preprocessor is capable to convert a C#-Object into a JS object, then you can do this. If not, you can't because you won't be able to access the values of the array. Commented Jun 4, 2012 at 13:15
  • For templates you can do jsfiddle.net/kut9R Commented Jun 4, 2012 at 13:25

2 Answers 2

1

As i posted in my comment, this is a bit tricky. However, you can try to construct a javascript object with your c#.

Something like this (i don't know how this works exactly...):

var array = [
   @for(var i = 0; i < Model.list.Length-1; i++){  @Model.list[i] , }
   @Model.list[length]
]

which should result in:

var array = [
         val1,
         val2,
         val3,
         valn
] 

Now you have an js var array, you can work with in your entire document.

Sign up to request clarification or add additional context in comments.

1 Comment

It should be noted that if those values are strings, it will not auto-generate the quotes and so they'll be processed as variables instead of literal strings. Otherwise, nice, clean answer.
0

You can't do it exactly that way. What you can do is use the C# to generate the javascript code for each line like this:

//...javascript code...

@for(var i = 0; i < Model.list.Length; i++)
{
    $("body").append("<h1></h1>").html('@(Model.list[i])');
}

//...more javascript code...

This should output like this:

//...javascript code...

$("body").append("<h1></h1>").html(listitem0);
$("body").append("<h1></h1>").html(listitem1);
$("body").append("<h1></h1>").html(listitem2);
//etc.

//...more javascript code...

2 Comments

i guess it's cleaner to construct the js-array and loop over it.
I would also put line inside for loop inside <text> tag.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.