美好的一天:

我得到了这段代码以在MVC 4中生成gridview;

@{SATXmlReports.Models.User spp = new SATXmlReports.Models.User();
           var grid = new WebGrid(ViewData["GridUserData"] as IEnumerable<SATXmlReports.Models.User>, canPage: true, canSort: true, rowsPerPage: 10, selectionFieldName: "selectedRow", ajaxUpdateContainerId: "gridContent");
          }
          <div id="gridContent" >
          @grid.GetHtml(
                        tableStyle: "table table-hover",
                        columns: grid.Columns(
                        grid.Column("Name", format: (item) => item.GetSelectLink(item.Name.ToString())),
                        grid.Column("Rol","Rol"),
                                 grid.Column("User Sites", "UserSites", format: item => Html.DropDownList("Sites", SATXmlReports.Models.User.GetUserSites(item.Name.ToString()).Select(u => new SelectListItem { Text = u.Description, Value = u.Number }))
                                 )))


但是我得到这个错误:


  错误11最佳重载方法匹配
  'System.Web.Helpers.WebGrid.Column(字符串,字符串,
  System.Func,string,bool)'有一些无效
  参数c:\ Users \ fernando.martinez \ Desktop \ SATXmlReports \ SATXmlReports \ Views \ Configuracion \ UserSettings.cshtml 109 38 SATXmlReports


和:


  错误12参数3:无法从“ lambda表达式”转换为
  'System.Func'c:\ Users \ fernando.martinez \ Desktop \ SATXmlReports \ SATXmlReports \ Views \ Configuracion \ UserSettings.cshtml 109 83 SATXmlReports


我尽一切努力获取项目属性,但是失败了。这种方法有效,但已硬编码:

grid.Column("User Sites", "UserSites", format: item => Html.DropDownList("Sites", SATXmlReports.Models.User.GetUserSites("John.Burdon").Select(u => new SelectListItem { Text = u.Description, Value = u.Number }))


有什么建议么?

最佳答案

您必须提防将扩展方法与动力学(项目)一起使用。

尝试这个:

grid.Column("User Sites", "UserSites", format: item => Html.DropDownList("Sites", SATXmlReports.Models.User.GetUserSites((string)item.Name).Select(u => new SelectListItem { Text = u.Description, Value = u.Number }))

10-08 01:09