68

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
yizzlez
  • 8,757
  • 4
  • 29
  • 44
Ashwani K
  • 7,880
  • 19
  • 63
  • 102

4 Answers4

110

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.

yizzlez
  • 8,757
  • 4
  • 29
  • 44
PanJanek
  • 6,593
  • 2
  • 34
  • 41
  • 55
    I just want to add that if you leave out the last parameter (the HTML attributes) MVC 3 will think the last parameter contains the HTML attributes. So don't forget an empty new { } as last parameters if you don't have any HTML attributes to set. – TheGuest Aug 07 '11 at 11:17
  • 6
    Instead of putting empty {} for the fourth parameter, we can use null. – Sugar Bowl May 05 '14 at 17:22
  • 1
    In razor, you need quotes, like this: new { id = id, data="name" } – TTT May 08 '15 at 21:48
  • Bless you for including the "style" with the example. – granadaCoder Aug 07 '15 at 19:05
5

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
Sanam Tiwari
  • 51
  • 1
  • 2
  • You got the "Home" and "Delete" the wrong way round i guess, because "Home" suppose to be a controller and "Delete" suppose to be action @Sanam Tiwari – Photonic Dec 19 '18 at 13:54
3

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.

Harry Binnendyk
  • 141
  • 1
  • 2
  • _Harry_ - you can use [action link](https://stackoverflow.com/a/61273535/1042705) also ! – Irf Apr 17 '20 at 14:12
  • Very good solution if you want to add a query parameter like "Grid-Task.Name=1", where parameter name contains characters like '.' or '-' I don't know any way how to do with actionlink – laza Jun 15 '20 at 11:40
1

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.

Oleg Karasik
  • 959
  • 6
  • 17