我有动态列并编写动态样式,但收到错误:
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)
答案 0 :(得分:0)
我找到了解决方案!如果你想在ASP.NET MVC 5中使用动态WebGrid ,您可以在View
中使用此代码 @{
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]
。现在有效了