从模型获取数组到视图

发布于 2024-12-19 02:43:30 字数 522 浏览 1 评论 0原文

在我的模型中,我有一个 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 .Boolean[] ,我尝试过: lstLabs: @Model.lstLabs.slice() 但它不起作用,我得到了同样的东西......

任何人都可以帮助我?

提前致谢。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

一影成城 2024-12-26 02:43:30

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

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

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>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文