19

I have the following WebGrid in my ASP.NET MVC3 test application. It displays a list of customers:

@grid.GetHtml(
tableStyle: "grid",
headerStyle: "head",
alternatingRowStyle: "alt",
columns: grid.Columns
         (
         grid.Column(format: (item) => Html.ActionLink("Edit", "Details", new { id = item.id })),
         grid.Column("Address.CompanyName"),
         grid.Column("Address.City")
         )
)

The interesting part here is the Edit-link I've added in the first column. I would like to use the customers account number instead of the plain "Edit"-test. However, it causes me a great deal of problems to do so.

I've tried:

grid.Column(format: (item) => Html.ActionLink(item.AccountNumber.ToString(), "Details", new { id = item.id })),

However, it seems like there is something i don't understand about how this works because i keep getting this exception:

CS1502: The best overloaded method match for 'System.Web.Helpers.WebGrid.Column(string, string, System.Func<dynamic,object>, string, bool)' has some invalid arguments

Can anyone explain to me why this isn't working? What is the difference between "Edit" and item.AccountNumber.ToString() (apart from the spelling)?

I should note that the link works when using the "Edit"-text, and that AccountNumber is a long.

GEOCHET
  • 21,119
  • 15
  • 74
  • 98
Thomas
  • 193
  • 1
  • 1
  • 4

8 Answers8

28

Here is an example of how I do it with a date.

grid.Column(columnName: "Date", format: (item) => Html.ActionLink(((string)item.Date), "Edit", new { id = item.id })),          

You have to beware of using extension methods (Html.*) with dynamics (item)... it doesn't work well in csharp. When you do the new {} projection or call ToString, it's no longer dynamic. Alternatively, you could cast: (object)item.Id.

From here.

Community
  • 1
  • 1
Derek Beattie
  • 9,429
  • 4
  • 30
  • 44
  • 1
    You've got it spot on! Thanks! If i used "(string)(item.AccountNumber.ToString()) it works... not quite sure why i need to cast it to a string though? I guess ToString() returns a string? – Thomas May 29 '11 at 13:57
  • I "think" item is dynamic so it doesn't have that method? – Derek Beattie May 29 '11 at 15:35
5

Just in case any one was wondering about how it should look in VB here is an example:

grid.Column("PersonID", "Admin", Function(modelItem) Html.ActionLink("View", "Details", New With {.id = modelItem.PersonID}))
2

In my case Derek Beattie solution is not working.

And I use this

 grid.Column(format: (item) => Html.ActionLink("Edit", "Edit", new { id = item.ID }), style: "column-action") 
Tieson T.
  • 20,774
  • 6
  • 77
  • 92
NoWar
  • 36,338
  • 80
  • 323
  • 498
1

My columns were being generated in the Model, where Html.ActionLink seems inaccessible. So I had to create the href and return it as an MvcHtmlString. This is what I ended up doing,

new WebGridColumn{ColumnName="FileName", Header= "File",
                Format = item => new MvcHtmlString("<a href='" + item.FileLink + "'>" + item.FileName +"</a>")
lanternmarsh
  • 683
  • 6
  • 8
0

I have solved like this

grid.Column("Id", format: (item) => Html.ActionLink((string)item.id.ToString(), "Edit", new { id = item.id }))

0

I use following code for grid. It working for me.

@grid.GetHtml(
    columns: grid.Columns(
        grid.Column(header: "Serial No", format:@<text><div>@(item.WebGrid.Rows.IndexOf(item) + 1)</div></text>),
        grid.Column(columnName: "Stdname", header: "Student Name"),
        grid.Column(header: "Email ID", format:@<text><a href="mailto:@item.Email">@item.Email</a></text>),
        grid.Column(header: "EDIT",format: (item) => Html.ActionLink("Edit", "Edit", new { id = item.ID })),
        grid.Column(header: "DETAILS", format: (item) => Html.ActionLink("Details", "Details", new { id = item.ID })),
        grid.Column(header: "DELETE", format: (item) => Html.ActionLink("Delete", "Delete", new { id = item.ID }))
))

Hope this is helpful.

Andy Rocks
  • 31
  • 4
0
 @Html.Grid(Model).Columns(columns => {
    columns.Add(c => c.ConsumerNo).Titled("Consumer No").SetWidth(70).Filterable(true);
    columns.Add(c => c.ConsumerName).Titled("Consumer Name").SetWidth(200).Filterable(true);
    columns.Add(c => c.MobileNo).Titled("Mobile No").SetWidth(70).Filterable(true);
    columns.Add(c => c.Address).Titled("Address").SetWidth(200).Filterable(true);
    columns.Add(c => c.AreaName).Titled("Area Name").SetWidth(70).Filterable(true);
    columns.Add(c => c.StaffName).Titled("Staff Name").SetWidth(100).Filterable(true);
    columns.Add().Encoded(false).Sanitized(false).Titled("INSPECT").SetWidth(60).RenderValueAs(o => Html.ActionLink("INSPECT", "InspForm", new { id = o.UniqueConsumerId, style = "background-image:url('~/Images/orderedList1.png')" }));                       
}).WithPaging(10).Sortable(true)
Thomas Bormans
  • 5,156
  • 6
  • 34
  • 51
-1
 grid.Column("GiftID",canSort:false, format: (item => Html.ActionLink((string)(@item.GiftID).ToString(), "Edit", new { GiftID = @item.GiftID })))
  • 1
    This answer does not help to answer the question. It provides a solution and the syntax appears to be correct but you should further explain why you would use this solution and attempt to answer the question and or explain why you would use this solution and why the previous syntax was not working. – Eric Bishard Sep 08 '15 at 09:23