【发布时间】:2011-07-04 14:35:32
【问题描述】:
我正在尝试使用 ASP.NET MVC3 中的新 WebGrid 以及我想要显示一组执行各种操作(编辑、查看、删除)的链接图标的列之一... 为此,我有一个 HtmlHelper 扩展,它基本上输出以下 HTML:
<a href="" title=""><img alt="" src="" /></a>
扩展返回 MvcHtmlString 并且在 Razor 视图中单独使用时可以正常工作。例如: @Html.ActionLinkIconForEditAction("客户", 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 和 this thread as well 的所有文档,但仍然无法弄清楚:)
谁能发现我做错了什么?
【问题讨论】:
-
更新:根据这篇文章,在新的 WebGrid 中使用动态对象可能存在问题...stackoverflow.com/questions/4195440/…
标签: c# lambda asp.net-mvc-3 webgrid