【问题标题】:Cannot use a lambda expression不能使用 lambda 表达式
【发布时间】:2015-10-08 15:59:37
【问题描述】:

我有动态列并编写动态样式但收到错误:

CS1977 C# 不能将 lambda 表达式用作动态分派操作的参数,除非先将其转换为委托或表达式树类型。

查看:

   WebGrid grid = new WebGrid(Model);
    List<WebGridColumn> columnsL = new List<WebGridColumn>();

    for (int i = 0; i < Model.First().Data.Count; i++)
    {
        int local = i;
        columnsL.Add(grid.Column(Model.First().Data[i].Name, Model.First().Data[i].Label,format: item => new MvcHtmlString("<text>" + item.Data[local].Value +"</text>")
    ));
    }


@grid.GetHtml(tableStyle: "table table-striped table-bordered", columns: columnsL)

【问题讨论】:

标签: c# asp.net-mvc


【解决方案1】:

我找到了解决方案!如果你想在 ASP.NET MVC 5 中使用动态 WebGrid ,您可以在视图中使用此代码

     @{
            WebGrid grid = new WebGrid(Model);
            List<WebGridColumn> columnsL = new List<WebGridColumn>();
    foreach (ServiceReference1.Column col in ViewBag.ColumnList)
            {
                WebGridColumn c = new WebGridColumn();
                c.ColumnName = col.columnName;
                c.Header = col.columnCaptionValue;
                c.Format = (item) => @Html.Raw("<div style='word-wrap:hyphenate;height:30px;min-width:" + col.columnWidth + "px'><b style='background-color:palevioletred'>" + item[col.columnName] + "</b></div>");

                columnsL.Add(c);
            }
}
    <div class="test" style="overflow: scroll;">
            <div style="width:1000px"> 
        @grid.GetHtml(tableStyle: "table table-striped table-bordered", columns: columnsL, caption: ViewBag.GridHeader, headerStyle: "gvHeading")
            </div>
        </div>

我的问题是item,但我解决了item[col.columnName]。它现在可以工作了

【讨论】:

    猜你喜欢
    • 2014-06-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-04-03
    • 1970-01-01
    • 2016-02-16
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多