How to pass query string parameter in ActionLink in MVC

asp.net Mvc

asp.net Mvc Problem Overview


I am having following action link:

<%= Html.ActionLink("Check this", "Edit", "test", 
                     new { id = id }, new { style = "display:block" })%>

How do I include data=name as query string. Some thing like this:

link?data=name

asp.net Mvc Solutions


Solution 1 - asp.net Mvc

4th parameter of Html.ActionLink can have any number of properties:

<%= Html.ActionLink("Check this", "Edit", "test", 
                     new { id = id, data=name }, new { style = "display:block" })%>

These properties are inserted into URL based on routing, but if the property name cannot be matched into any route it is added as URL GET parameter.

So if you have standard route {controller}/{action}/{id}, you will get the URL:

test/Edit/[id]?data=[name] 

from the above code.

Solution 2 - asp.net Mvc

Pass Query String By this way

@Html.ActionLink("Delete Record", "Home", "Delete", new { id=Id},null)

By above code you will get the url like(Suppose Id=1): /Home/Delete/1

and if you want to add more parameters to query string then:

@Html.ActionLink("Delete Record", "Home", "Delete", new { id=Id, Name=name},null)

By above code you will get the url like(Suppose Id=1 and Name=India) :

/Home/Delete/1?Name=India

Solution 3 - asp.net Mvc

I got tired of banging my head against a wall with the html.actionlink. Works great when you just want to route it against straightforward routing calls, but absolutely refuses to cooperate when you want to add a simple querystring at the end.

I don't an ID at then end, I want to be able to add some kind of actual Querystring with the "?".

So anywhere I needed a Querystring I switched to using the url.action inside the anchor tag.

<a href='@url.action("Action","route")?Parameter=Value' >Text for Link Name</a>

At least it works and I can stop getting headaches over something that should have been a very simple task. Someone needs to get their heads out of their butts and make the ActionLink work properly for Querystrings in the MVC routing.

Solution 4 - asp.net Mvc

I know this is kind of old question but.

In case the below code doesn't generate the <a href="/?param=value" />.

<%= Html.ActionLink("Text", "Action", "Controller", new { param=value }, null)%>

I would advice checking whether you action has at least one [Route] attribute (I used [Route("/")] for example).

Hope it helps.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionAshwani KView Question on Stackoverflow
Solution 1 - asp.net MvcPanJanekView Answer on Stackoverflow
Solution 2 - asp.net MvcSanam TiwariView Answer on Stackoverflow
Solution 3 - asp.net MvcHarry BinnendykView Answer on Stackoverflow
Solution 4 - asp.net MvcOleg KarasikView Answer on Stackoverflow