12

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>

The extension returns MvcHtmlString and it works fine when used in Razor views by itself..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. 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>)

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

Can anyone spot what I am doing wrong?

Community
  • 1
  • 1
zam6ak
  • 7,229
  • 11
  • 46
  • 84
  • Update: according to this post, there may be an issue with using dynamic objects in new WebGrid...http://stackoverflow.com/questions/4195440/mvc-3-texbox-in-webgrid-razor – zam6ak Feb 25 '11 at 21:53

3 Answers3

15

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)

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

Regards Z...

zam6ak
  • 7,229
  • 11
  • 46
  • 84
  • Thanks, I came across this looking for info on formatting a column with my own custom html. the difference is that I'm using the default view engine and struggled to find an equivalent of the "@@Html..." notation, so what I did was to create an MvcHtmlString: grid.Column("Field","Header", item => MvcHtmlString.Create(string.Format("

    {0}

    ", item.Field))) Thought I'd share. Is there a better way? please point me to it :)
    – Veli Gebrev May 03 '11 at 10:41
1
grid.Column(format: (item) => Html.ActionLink("EditCustomer","Editcustomer", new {CustomerId=item.CustomerId}))

This will work ,and above one also work.. Here 1st "EditCustomer" is the text to be visible to users , second "EditCustomer" is the name of action you want it to redirect , and item is

1

I had to create a WebGrid in C# code and had to display a specific column using hyperlink. 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.


  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.

Marko
  • 20,385
  • 13
  • 48
  • 64
Ajay
  • 11
  • 2