【问题标题】:How to populate a WebGrid cell with a collection of ActionLinks如何使用一组 ActionLinks 填充 WebGrid 单元格
【发布时间】:2016-10-06 22:37:39
【问题描述】:

我有一个这样的模型:

class Model
{
    public String Text { get; set; }
    public AnotherType[] Values { get; set; }
}

还有一个带有IEnumerable<Model> 的视图,我使用 Razor 和 asp.net MVC 4 在 WebGrid 中显示该视图。

到目前为止我有:

@{ var grid = new WebGrid(Model); }
<div id="grid">
      @grid.GetHtml(
          columns: 
              grid.Columns(
                  grid.Column("Text")))
</div>

我想要的是根据第二列中我的模型中Values 的值创建一个逗号分隔的链接列表。

我尝试创建一个 Linq 查询将它们投影到 ActionLinks 中(或者甚至是一个字符串数组并使用 String.Join() 来测试它),但这在运行时无法编译。

例如:

grid.Column("Values",
    format: (item) => String.Join(item.Select(v => v.Property).ToArray()))))

我得到的错误是(我认为):

错误 CS1977:不能使用 lambda 表达式作为参数 动态调度操作,无需先将其转换为 委托或表达式树类型

我希望我的输出看起来像:

|--------------------------------| |**文字** | **价值观** | |--------------------------------| |文字A |瓦尔 1、 瓦尔 2、 瓦尔 3 | |--------------------------------|

Val n 是一个链接,可以让您离开以查看更多详细信息。

有谁知道这样做的方法吗?

作为一种解决方法,我将模型中的值组合起来,并在结果中公开一个String 属性,但这不允许我创建ActionLinks。

【问题讨论】:

    标签: asp.net-mvc razor webgrid


    【解决方案1】:

    您必须将项目(动态)转换为 AnotherType

    (item).Cast<AnotherType>()
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-11-04
      • 1970-01-01
      • 1970-01-01
      • 2015-06-04
      • 1970-01-01
      • 1970-01-01
      • 2018-07-08
      • 1970-01-01
      相关资源
      最近更新 更多