[英]ASP.NET MVC3 WebGrid format: parameter
I am trying to use the new WebGrid in ASP.NET MVC3 and one of the columns I want to display set of link icons that performs various actions (Edit, View, Delete)... For this purpose I have a HtmlHelper extensions that basically outputs following HTML: 我试图在ASP.NET MVC3中使用新的WebGrid,并且我想要显示一组链接图标,执行各种操作(编辑,查看,删除)...为此,我有一个HtmlHelper扩展,基本上输出HTML:
<a href="" title=""><img alt="" src="" /></a>
The extension returns MvcHtmlString and it works fine when used in Razor views by itself..Eg: @Html.ActionLinkIconForEditAction("Customer", 2) 扩展返回MvcHtmlString,它在Razor视图中单独使用时工作正常.Eg:@ Html.ActionLinkIconForEditAction(“Customer”,2)
The issue is I need to invoke this helper (once for each action) in WebGrid column while passing the ID of the object. 问题是我需要在传递对象的ID时在WebGrid列中调用此帮助程序(每个操作一次)。 The problem that I am stumped on is that compiler gives me an error saying that it cannot convert MvcHtmlString (or 'lambda expression' depending on invocation I try) to System.Func expected by the format...
我难以理解的问题是编译器给我一个错误,说它不能将MvcHtmlString(或'lambda expression'取决于我尝试的调用)转换为格式预期的System.Func ...
So for example, this works: 例如,这有效:
grid.Column(header: "", format: @<text>@Html.ActionLinkIconForEditAction("Customer", 2)</text>)
But this does not: 但这不是:
grid.Column(header: "", format: (customer) => @<text>@Html.ActionLinkIconForEditAction("Customer", customer.Id)</text>)
grid.Column(header: "", format: (customer) => Html.ActionLinkIconForEditAction("Customer", customer.Id))
I get: 我明白了:
Error 4 Argument 3: cannot convert from 'lambda expression' to 'System.Func<dynamic,object>'
or for this call: 或者这个电话:
grid.Column(header: "", format: Html.ActionLinkIconForEditAction("Customer", customer.Id)),
I get: 我明白了:
Error 5 Argument 3: cannot convert from 'System.Web.Mvc.MvcHtmlString' to 'System.Func<dynamic,object>'
What is weird I have other columns that ustilize lambdas, direct Model.Property accessors, and even output from String.Format("")...They all work fine... I read all the docs on Func and this thread as well , and still can't quite figure it out :) 有什么奇怪我有其他列使用lambdas,直接Model.Property访问器,甚至从String.Format(“”)输出...它们都工作正常...我读了Func上的所有文档和这个线程也是如此 ,还是不能弄清楚:)
Can anyone spot what I am doing wrong? 谁能发现我做错了什么?
I got it :) .... The issue appears to be in the way C# handles dynamic objects...Lots of users are having a blast with this... 我明白了:) ....问题似乎在于C#处理动态对象的方式......很多用户都在忙着这个......
The fix was as simple as casting a correct type on the parameter to my extension helper... So this works: 修复就像将参数的正确类型转换为我的扩展帮助程序一样简单......所以这样做:
grid.Column(header: "", format: @<text>@Html.ActionLinkIconForEditAction("Customer", (int)item.Id)
The other trick is to use "built" in "item" object and not provide your own..So, for example, this does not work: 另一个技巧是在“item”对象中使用“built”而不提供你自己的..例如,这不起作用:
grid.Column(header: "", format: (customer) => @<text>@Html.ActionLinkIconForEditAction("Customer", (int)customer.Id)
Lots of reading, tweaking, learning... Hopefully, next version will have something a lot easier to use when you need to add content to columns that are not coming directly from the model... 大量的阅读,调整,学习...希望下一版本在您需要向不直接来自模型的列添加内容时会有更多的内容...
Regards Z... 关心Z ......
grid.Column(format: (item) => Html.ActionLink("EditCustomer","Editcustomer", new {CustomerId=item.CustomerId}))
这将工作,并且上面的一个也工作..这里第一个“EditCustomer”是用户可见的文本,第二个“EditCustomer”是你想要它重定向的动作的名称,而item是
I had to create a WebGrid
in C# code and had to display a specific column using hyperlink. 我不得不用C#代码创建一个
WebGrid
,并且必须使用超链接显示一个特定的列。 None of the techniques mentioned above worked for me. 上面提到的技术都不适合我。 So, I used
MvcHtmlString.create
and passed in the return value of String.format
to it. 所以,我使用了
MvcHtmlString.create
并将String.format
的返回值传递给它。
format: (item) => MvcHtmlString.Create(string.Format((A HREF=\"/Inventory/EditInventory/{0}\"){1}(/A)", item.ID, item.Property)))
Replace the ( or ) inside the string with < or > respectively. 分别用<或>替换字符串内的(或)。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.