Mano Johnbritto
Mano Johnbritto

Reputation: 308

How to use model int array variable in javascript MVC 4

Model

public int[] mobileid{get;set;}

Script

var k;
for(var i=0;i<3;i++)
{
  [email protected][i];
  alert(k);
}

This method not working,How to assign int array variable to Javascript var. I already use below method:

var k=[];
[email protected](json.Encode(model.mobileid));//not work

Upvotes: 1

Views: 500

Answers (1)

Oluwafemi
Oluwafemi

Reputation: 14889

You can convert your Model.mobileid to a string with a separator of your choice and convert it to a javascript array:

<script>
    var k = "@(Model.mobileid == null ? string.Empty : string.Join(",", Model.mobileid))";
    alert(k);
    k = k.split(",");
    k // k is an array at this point
</script>

Upvotes: 2

Related Questions