Create an ActionLink for a DisplayFor value

1.5k views Asked by At

I have coded a C# MVC 5 Internet Application and have a question about an ActionLink in a View.

I have the following code:

<td>
    @Html.DisplayFor(modelItem => item.mapLocations.Count)
</td>

Is it possible to create an ActionLink for this above displayed value?

Thanks in advance

EDIT

I have tried the following code:

@Html.ActionLink(item.mapLocations.Count, "Index", "MapLocation", new { mapCompanyid = item.Id }, null)

I am getting a compilation error as follows:

Compiler Error Message: CS1928: 'System.Web.Mvc.HtmlHelper<CanFindLocation.ViewModels.MapCompaniesViewModel>' does not contain a definition for 'ActionLink' and the best extension method overload 'System.Web.Mvc.Html.LinkExtensions.ActionLink(System.Web.Mvc.HtmlHelper, string, string, string, object, object)' has some invalid arguments

EDIT2

Do I need the DisplayFor code to display the value in the ActionLink?

Maybe something like this:

@Html.ActionLink(Html.DisplayFor(modelItem => item.mapLocations.Count), "Index", "MapLocation", new { mapCompanyid = item.Id }, null)
1

There are 1 answers

8
Kartikeya Khosla On BEST ANSWER

This will work :-

@Html.ActionLink(item.mapLocations.Count, "// action name //", "// controller name //", new{ mapCompanyid = item.Id  }, new{ // html attributes // })

Just match your Actionlink it's structure should look like below :-

Html.ActionLink(item.mapLocations.Count, //  <--Title
            "Index",   // <-- ActionMethod
            "MapLocation",  // <-- Controller Name.
            new { mapCompanyid = item.Id }, // <-- Route arguments.
            null  // <-- htmlArguments.
            )