user4864716
user4864716

Reputation:

In MVC Razor, how can I correctly add querystring parameters to an html.actionlink?

I have an MVC Razor page where I am trying to add a querystring parameter to the URL.

<td>                
     @Html.ActionLink(item.Student_Name, "Index", "FourCourseAuditDetails", 
           new { filterByStudent = item.Student_Name})
</td>

My desired outcome is: http://[server]/FourCourseAuditDetails/Index?filterByStudent="[item.Student_Name]", but when I test out the page, the anchor href attirubte looks like this: http://[server]/[route of current view]?length=[integer value]

According to the Intellsense and the Html.ActionLink descriptions, I am using the correct arguments, but clearly something is not right.

The thing that came to mind is that item.Student_Name is a string that contains spaces and commas. I tried filterByStudent = item.Student_Name.toString(), which didn't help. I still think that's the cause of the problem, but I don't know what else I can do. Any ideas?

Upvotes: 0

Views: 1306

Answers (1)

Satpal
Satpal

Reputation: 133403

The problem arise from the fact that there is no overload as LinkExtensions.ActionLink Method (HtmlHelper, String, String, String, Object)

Use the overloaded method LinkExtensions.ActionLink Method (HtmlHelper, String, String, String, Object, Object)

@Html.ActionLink((string)item.Student_Name, "Index", "FourCourseAuditDetails", 
       new { filterByStudent = item.Student_Name}, null)

Also need to type cast item.Student_Name to string.

Upvotes: 3

Related Questions