Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get array from model to view

In my model I have one int object and a boolean array:

public class mymodel
{
  public int Round { get; set; }
  public Boolean[] lstLabs { get; set; }
}

In my view I write this :

<script type="text/javascript">
var objModel = {  
    Round:"@Model.Round",
    lstLabs: "@Model.lstLabs"
      }; 
</script>

I get only the value of Round (the int object) , but I can't get the array , I just get this : lstLabs : System.Boolean[] , I tried : lstLabs: @Model.lstLabs.slice() but it didn't work , I got the same thing...

Can anyone help me ?

Thanks in advance.

like image 357
ParPar Avatar asked Dec 01 '11 07:12

ParPar


1 Answers

If you want all properties of the view model:

<script type="text/javascript">
    var objModel = @Html.Raw(Json.Encode(Model));
    alert(objModel.Round + ' ' + objModel.lstLabs.length);
</script>

or if you want only a subset:

<script type="text/javascript">
    var objModel = @Html.Raw(Json.Encode(new {
        Labs = Model.lstLabs
    }));
    alert(objModel.Labs.length);
</script>
like image 63
Darin Dimitrov Avatar answered Oct 02 '22 23:10

Darin Dimitrov