ASP.NET MVC3的WebGrid格式:参数 [英] ASP.NET MVC3 WebGrid format: parameter

查看:127
本文介绍了ASP.NET MVC3的WebGrid格式:参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用新的WebGrid在ASP.NET MVC3和列一个我想要显示的设置执行各种操作(编辑,查看,删除)链接图标...
为此,我有一个扩展的HtmlHelper基本上输出以下HTML:

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:

<a href="" title=""><img alt="" src="" /></a>

扩展返回MvcHtmlString和itself..Eg在剃刀意见使用时正常工作:
@ Html.ActionLinkIconForEditAction(客户,2)

The extension returns MvcHtmlString and it works fine when used in Razor views by itself..Eg: @Html.ActionLinkIconForEditAction("Customer", 2)

问题是我需要的WebGrid列来调用这个帮手(每进行一次操作),同时传递对象的ID。那我就为难的问题是,编译器给我的预期格式的错误说,它不能转换MvcHtmlString(或'拉姆达前pression这取决于调用我尝试),以System.Func ...

The issue is I need to invoke this helper (once for each action) in WebGrid column while passing the ID of the object. 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...

因此​​,例如,这个作品:

So for example, this works:

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>'

什么是奇怪我有ustilize lambda表达式,直接Model.Property访问,甚至可以从的String.Format()输出等栏目......他们都做工精细...
我读了Func键所有的文档和这个线程以及,仍然不能相当看着办吧:)

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 :)

任何人能发现我在做什么错了?

Can anyone spot what I am doing wrong?

推荐答案

我知道了:) ....
这个问题似乎是在路上C#处理动态对象...用户大量有与此爆炸...

I got it :) .... The issue appears to be in the way C# handles dynamic objects...Lots of users are having a blast with this...

此修复程序是铸造的参数正确的类型我扩展帮助简单...
所以这个作品:

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)

另一个技巧是使用建,在项目对象,而不是提供您own..So,例如这不起​​作用:

The other trick is to use "built" in "item" object and not provide your own..So, for example, this does not work:

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...

问候
Z ...

Regards Z...

这篇关于ASP.NET MVC3的WebGrid格式:参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆