ParPar
ParPar

Reputation: 7549

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.

Upvotes: 5

Views: 2104

Answers (1)

Darin Dimitrov
Darin Dimitrov

Reputation: 1038770

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>

Upvotes: 7

Related Questions