Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Url.Action parameters?

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> 
like image 372
user787788 Avatar asked Jun 08 '11 12:06

user787788


People also ask

What is a URL action?

A URL action is a hyperlink that points to a web page, file, or other web-based resource outside of Tableau. You can use URL actions to create an email or link to additional information about your data. To customize links based on your data, you can automatically enter field values as parameters in URLs.

How do you pass dynamic values in URL action?

You can concat the client-side variables with the server-side url generated by this method, which is a string on the output. Try something like this: var firstname = "abc"; var username = "abcd"; location. href = '@Url.

How do I set an area in URL action?

You can use this Url. Action("actionName", "controllerName", new { Area = "areaName" }); Also don't forget to add the namespace of the controller to avoid a conflict between the admin area controller names and the site controller names.

What is action parameters in MVC?

Action Method Parameters are most important in MVC. If you want to handle post request in action methods; MVC framework provided types of Action Methods Parameters. Action Method Parameters. We can organize the action methods for GET and POST requests separately.


2 Answers

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)  {     ... } 
like image 71
Darin Dimitrov Avatar answered Oct 12 '22 09:10

Darin Dimitrov


This works for MVC 5:

<a href="@Url.Action("ActionName", "ControllerName", new { paramName1 = item.paramValue1, paramName2 = item.paramValue2 })" >     Link text </a> 
like image 28
César León Avatar answered Oct 12 '22 08:10

César León