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(" rel="nofollow noreferrer noopener" rel="nofollow noreferrer noopener"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(" rel="nofollow noreferrer noopener" rel="nofollow noreferrer noopener"GetByList","Listing" , new {name= "John" , contact= " calgary, vancouver" })%>"><span>People</span></a>
Answers:
Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.
Method 1
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(" rel="nofollow noreferrer noopener" rel="nofollow noreferrer noopener"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&contact=calgary%2C%20vancouver" rel="nofollow noreferrer noopener">
<span>People</span>
</a>
which will successfully invoke the GetByList controller action passing the two parameters:
public ActionResult GetByList(string name, string contact)
{
...
}
Method 2
This works for MVC 5:
<a href="@Url.Action(" rel="nofollow noreferrer noopener"ActionName", "ControllerName", new { paramName1 = item.paramValue1, paramName2 = item.paramValue2 })" >
Link text
</a>
Method 3
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(" rel="nofollow noreferrer noopener" rel="nofollow noreferrer noopener"GetByList", "Listing")%>?<%:qs%>">
<span>People</span>
</a>
Method 4
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¶m3=ViewBag.P3'>Print</a>
Method 5
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>
All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0