从模型到视图获取数组

Get array from model to view

本文关键字:获取 数组 视图 模型      更新时间:2023-09-26

在我的模型中,我有一个int对象和一个布尔数组:

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

在我看来,我是这样写的:

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

我只得到Round的值(int对象),但我不能得到数组,我只得到这个:lstLabs: System。布尔[],我尝试:lstLabs: @Model.lstLabs.slice(),但它没有工作,我得到了同样的东西…

有人能帮我吗?

如果你想要视图模型的所有属性:

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

或者如果您只想要一个子集:

<script type="text/javascript">
    var objModel = @Html.Raw(Json.Encode(new {
        Labs = Model.lstLabs
    }));
    alert(objModel.Labs.length);
</script>