Multiple Html.ActionLink() within single MvcContrib.Grid column in MVC3

4.4k views Asked by At

I'm trying to wrap my head around the proper syntax to achieve the following--or at least to find out if it's even do-able or if there is a better alternative solution.

Ok, out of the box, when you generate a strongly-typed View using the List scaffold under ASP.Net MVC3 you get a simple table with a column that has something like two or three Html.ActionLink() items representing common actions like so: Edit | Details | Delete

I would like to use the MvcContrib grid and do the same, but I cannot figure out the correct syntax to get it to work. So far, in my Index.cshtml, I have the following snippet:

@(
        Html.Grid(Model.PagedList).AutoGenerateColumns()
            .Columns(column => 
            {
                column.For(f => Html.ActionLink("Edit", "Edit", new { id = f.itemID}))
                .Named("");
            })
            .Sort(Model.GridSortOptions);
    )

but that just gives me one column for "Edit", where as I want the column to contain three action links--Edit, Devices, Delete--with all three having the same itemID for the particular row. Is this achievable? And if so, how? If not, is there an alternative?

3

There are 3 answers

2
Darin Dimitrov On BEST ANSWER

You could use a custom column:

columns.Custom(
    @<text>
        @Html.ActionLink("edit", "edit", new { id = item.Id }) |
        @Html.ActionLink("details", "details", new { id = item.Id }) |
        @Html.ActionLink("delete", "delete", new { id = item.Id }) |
    </text>
);
2
SLaks On

You should pass an inline helper:

column.For(@<text>
    @Html.ActionLink("Edit", "Edit", new { id = item.itemID })
    @Html.ActionLink(...)
    @Html.ActionLink(...)
</text>)
0
user2393156 On

Maybe it helps somebody:

@Html.Grid(Model).Columns(column =>
    {
        column.For( c=> Html.Raw(Html.ActionLink(...).ToString() + 
            " " + Html.ActionLink(...).ToString())).Named("Actions").Encode(false);
....