Parameters Url.Action?

In the controller listing, 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)); } 

On an 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 ASPX code ... I can pull the entries for the name john. but when I give contact = "calgary, vancouver", the webpage goes into error.

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

  <a href="<%:Url.Action("GetByList","Listing" , new {name= "John" , contact= " calgary, vancouver" })%>"><span>People</span></a> 
+79
html asp.net-mvc-2 url.action
Jun 08 '11 at 12:29
source share
2 answers

The following is the correct overload (in your example, you don’t have a closing } anonymous routeValues object routeValues that your code routeValues 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 call the action of the GetByList controller, passing two parameters:

 public ActionResult GetByList(string name, string contact) { ... } 
+140
Jun 08 '11 at 12:32
source share

you can return a private collection with the name HttpValueCollection, even the documentation says that it has the name NameValueCollection using the ParseQueryString utility. Then add the keys manually, HttpValueCollection will do the encoding for you. And then just add 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> 
+2
Mar 27 '13 at 23:12
source share



All Articles