Url.Action parameters?
Asked Answered
A

5

144

In listing controller I have,

 public ActionResult GetByList(string name, string contact)
 {        
     var NameCollection = Service.GetByName(name);    
     var ContactCollection = Service.GetByContact(contact);           
     return View(new ListViewModel(NameCollection ,ContactCollection));
 }

In ASPX page I call,

 <a href="<%:Url.Action("GetByList","Listing" , new {name= "John"} , new {contact="calgary, vancouver"})%>"><span>People</span></a>

I have a problem in the ASPX code... I can pull the records for the name john. but when I give the contact="calgary, vancouver", the webpage goes to error.

How can I call two parameters in the Url.Action. I tried the below but that seems wrong too.

  <a href="<%:Url.Action("GetByList","Listing" , new {name= "John" , contact= " calgary, vancouver" })%>"><span>People</span></a>
Agamemnon answered 8/6, 2011 at 12:29 Comment(0)
I
249

The following is the correct overload (in your example you are missing a closing } to the routeValues anonymous object so your code will throw an exception):

<a href="<%: Url.Action("GetByList", "Listing", new { name = "John", contact = "calgary, vancouver" }) %>">
    <span>People</span>
</a>

Assuming you are using the default routes this should generate the following markup:

<a href="/Listing/GetByList?name=John&amp;contact=calgary%2C%20vancouver">
    <span>People</span>
</a>

which will successfully invoke the GetByList controller action passing the two parameters:

public ActionResult GetByList(string name, string contact) 
{
    ...
}
Infecund answered 8/6, 2011 at 12:32 Comment(6)
@user787788, what problem are you getting? What do you mean not accepting two parameters? Who is not accepting? Are you getting an error message? Is your controller action not hit? Are you getting wrong values? Be specific.Infecund
When i give two parameters, the aspx page is not even hitting the controller. its finding the error in the page itself. i have a defalut exception called something gone worng. the aspx goes thereAgamemnon
@user787788, what is the error? Exact message you are getting?Infecund
the error is , you may have typed the address (URL) incorrectly.Agamemnon
@DarinDimitrov, i know this is a very old post, but i saw you were online. I don't suppose you'd know how to pass a parameter dynamically (not hardcoded like "John")?Eugeniusz
@Eugeniusz Here is an example of what you can place in your view file: '@Url.Action("GetOverviewAsDocument", "FileAssignment", new { customerId = ViewData["customerId"] } )'Uta
Z
14

This works for MVC 5:

<a href="@Url.Action("ActionName", "ControllerName", new { paramName1 = item.paramValue1, paramName2 = item.paramValue2 })" >
    Link text
</a>
Zoarah answered 22/6, 2018 at 17:0 Comment(2)
What is item, as in item.paramValue1?Inexcusable
item.paramValue1 could be any text/string you wantPossibility
G
4

you can returns a private collection named HttpValueCollection even the documentation says it's a NameValueCollection using the ParseQueryString utility. Then add the keys manually, HttpValueCollection do the encoding for you. And then just append the QueryString manually :

var qs = HttpUtility.ParseQueryString(""); 
qs.Add("name", "John")
qs.Add("contact", "calgary");
qs.Add("contact", "vancouver")

<a href="<%: Url.Action("GetByList", "Listing")%>?<%:qs%>">
    <span>People</span>
</a>
Galleass answered 27/3, 2013 at 23:12 Comment(0)
F
1

Here is another simple way to do it

<a class="nav-link"
   href='@Url.Action("Print1", "DeviceCertificates", new { Area = "Diagnostics"})\@Model.ID'>Print</a>

Where is @Model.ID is a parameter

And here there is a second example.

<a class="nav-link"
   href='@Url.Action("Print1", "DeviceCertificates", new { Area = "Diagnostics"})\@Model.ID?param2=ViewBag.P2&param3=ViewBag.P3'>Print</a>
Fireproof answered 17/3, 2020 at 2:22 Comment(0)
O
0

You can also do the following at ASP.Net MVC

<a href="@Url.Action("GetByList", "Listing",new {area = "Admin"})'
+ @Model.Id + '?name ="John"&contact ="calgary, vancouver"'+'"'>
    <span>People</span>
</a>
Ogden answered 25/9, 2021 at 5:32 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.