Jaylen
Jaylen

Reputation: 40381

How can I pass parameter to a link generated by ActionLink() in ASP.NET MVC?

I have a route that I can access using the following pattern

ResponsesTotals/Show/1
ResponsesTotals/Show/2
.....
.....
ResponsesTotals/Show/n

From a different view, I need to list names and when the user clicks on them I want to direct them to the routes showing above.

Here is what I have tried

@model List<Proj.Model.Survey>

<ul>
    @foreach (var survey in Model)
    {
        <li>@Html.ActionLink(survey.Name, "Show", "ResponsesTotals", new { SurveyId = survey.Id })</li>
    }
</ul>

But that for some reason is generating wrong URLs. Here is what is being generated

ResponsesTotals/Show?Length=15

How can I pass a parameter to the link?

Upvotes: 2

Views: 38

Answers (1)

mxmissile
mxmissile

Reputation: 11681

Add null for the last parameter so you hit the correct method:

@Html.ActionLink(survey.Name, "Show", "ResponsesTotals", new { SurveyId = survey.Id },null)

Upvotes: 2

Related Questions