How to pass a query string parameter to ActionLink in MVC

I have a link for the action:

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

How to include data=name in the query string. Something like that:

 link?data=name 
+54
asp.net-mvc
Jun 23 '10 at
source share
2 answers

The 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 the URL based on routing, but if the name of the property cannot be matched to any route, it is added as a parameter of the GET URL.

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

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

from the code above.

+92
Jun 23 2018-10-06T00:
source share

Skip the query string this way

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

In the above code, you will get url like (Assume Id = 1): /Home/Delete/1

and if you want to add additional parameters to the query string, then:

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

In the above code, you will get url like (Assume Id = 1 and Name = India):

 /Home/Delete/1?Name=India 
0
Dec 26 '17 at 0:20
source share



All Articles