ASP.NET MVC3 WebGrid format: parameter
Asked Answered
C

3

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?

Consult answered 25/2, 2011 at 21:10 Comment(1)
Update: according to this post, there may be an issue with using dynamic objects in new WebGrid...#4195940Consult
C
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...

Consult answered 25/2, 2011 at 23:17 Comment(1)
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 "@<text>@Html..." notation, so what I did was to create an MvcHtmlString: grid.Column("Field","Header", item => MvcHtmlString.Create(string.Format("<p class=\"CssClass\">{0}</p>", item.Field))) Thought I'd share. Is there a better way? please point me to it :)Foolproof
S
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

Spinode answered 25/7, 2012 at 11:12 Comment(0)
G
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.

Gobbler answered 3/9, 2012 at 12:45 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.