我正在尝试在 ASP.NET MVC3 中使用新的 WebGrid 以及我想显示一组执行各种操作(编辑、查看、删除)的链接图标的列之一......为此,我有一个 HtmlHelper 扩展,基本上输出以下 HTML:
<a href="" title=""><img alt="" src="" /></a>
扩展返回 MvcHtmlString 并且在 Razor 视图中单独使用时可以正常工作。例如:@Html.ActionLinkIconForEditAction("Customer", 2)
问题是我需要在传递对象 ID 时在 WebGrid 列中调用此帮助程序(每个操作一次)。我被难住的问题是编译器给了我一个错误,说它无法将 MvcHtmlString(或“lambda 表达式”,取决于我尝试的调用)转换为格式所期望的 System.Func...
例如,这有效:
grid.Column(header: "", format: @<text>@Html.ActionLinkIconForEditAction("Customer", 2)</text>)
但这不会:
grid.Column(header: "", format: (customer) => @<text>@Html.ActionLinkIconForEditAction("Customer", customer.Id)</text>)
grid.Column(header: "", format: (customer) => Html.ActionLinkIconForEditAction("Customer", customer.Id))
我得到:
Error 4 Argument 3: cannot convert from 'lambda expression' to 'System.Func<dynamic,object>'
或者对于这个电话:
grid.Column(header: "", format: Html.ActionLinkIconForEditAction("Customer", customer.Id)),
我得到:
Error 5 Argument 3: cannot convert from 'System.Web.Mvc.MvcHtmlString' to 'System.Func<dynamic,object>'
奇怪的是,我还有其他列使用 lambda,直接 Model.Property 访问器,甚至是 String.Format("") 的输出......它们都工作正常......我阅读了 Func 和这个线程上的所有文档,但仍然无法弄清楚:)
谁能发现我做错了什么?