【问题标题】:Get array from model to view从模型获取数组到视图
【发布时间】:2012-01-10 09:41:54
【问题描述】:

在我的模型中,我有一个 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 (the int object) 的值,但我无法得到数组,我只得到这个:lstLabs : System.Boolean[],我试过了:lstLabs: @Model.lstLabs.slice() 但它没有用,我得到了同样的东西......

谁能帮帮我?

提前致谢。

【问题讨论】:

    标签: javascript asp.net-mvc-3 view model razor


    【解决方案1】:

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

    <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>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-08-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-04-26
      • 1970-01-01
      相关资源
      最近更新 更多