How to add ActionLink for grid

Aug 12, 2012 at 5:03 AM

I need add action link or submit button on grid.

Can you help me,

Thanks, :)

Coordinator
Aug 17, 2012 at 3:37 AM

Sure. Use RenderValueAs method, like this:

    @Html.Grid(Model).Columns(columns =>
                            {
                                columns.Add(o => o.OrderID)
                                    .Titled("Number")
                                    .RenderValueAs(o => Html.ActionLink("Edit", "Edit", "Home", new { Id = o.OrderID }, null));
                            })

Or you can specify htlm helper to render more complex layout:

    @helper GridManageItemColumn(int id)
    {
        @Html.ActionLink("Edit", "Edit", "Home", new { Id = id }, null)
        <span>,</span>
        @Html.ActionLink("Delete", "Delete", "Home", new { Id = id }, null)
    }
    @Html.Grid(Model).Columns(columns =>
                            {
                                columns.Add(o => o.OrderID)
                                    .Titled("Number")
                                    .RenderValueAs(o => GridManageItemColumn(o.OrderID).ToHtmlString());
                            })

Sep 3, 2012 at 5:30 PM

Trying to use your first suggestion above, but can't get it to work.

RenderValueAs expects a string, but Html.ActionLink() returns MvcHtmlString.
Any advice?

Coordinator
Sep 4, 2012 at 4:01 AM

Yes, sorry.

You need to cast action link to html string by ToHtmlString method:

     @Html.Grid(Model).Columns(columns =>
                            {
                                columns.Add(o => o.OrderID)
                                    .Titled("Number")
                                    .RenderValueAs(o => Html.ActionLink("Edit", "Edit", "Home", new { Id = o.OrderID }, null).ToHtmlString());
                            })

Sep 4, 2012 at 6:17 AM

This doesn't seem to work either. I get the html displayed as text, i.e.:

<a href="/Controller/Action">Link</a>

Coordinator
Sep 4, 2012 at 6:27 AM

You also need to disable encoding and sanitizing the content of the column, which enabled by default:

@Html.Grid(Model).Columns(columns =>
                        {
                            columns.Add(o => o.OrderID)
                                .Titled("Number")
                                .Sanitized(false)
                                .Encoded(false)
                                .RenderValueAs(o => Html.ActionLink("Edit", "Edit", "Home", new { Id = o.OrderID }, null).ToHtmlString());
                        })

Sep 4, 2012 at 6:32 AM

Thanks, that did it.