Using data in a HTML.ActionLink inside a WebGrid.column, not possible?

∥☆過路亽.° 提交于 2019-11-28 06:53:31
Derek Beattie

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.

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}))
Developer

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

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

I have solved like this

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

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.

Pushpraje96
 @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)
 grid.Column("GiftID",canSort:false, format: (item => Html.ActionLink((string)(@item.GiftID).ToString(), "Edit", new { GiftID = @item.GiftID })))
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!