Html Url.Action 参数?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6278694/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Url.Action parameters?
提问by user787788
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,
在我调用的 ASPX 页面中,
<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.
我的 ASPX 代码有问题...我可以提取名称 john 的记录。但是当我给 contact="calgary, vancouver" 时,网页会出错。
How can I call two parameters in the Url.Action. I tried the below but that seems wrong too.
如何在 Url.Action 中调用两个参数。我尝试了以下但似乎也错了。
<a href="<%:Url.Action("GetByList","Listing" , new {name= "John" , contact= " calgary, vancouver" })%>"><span>People</span></a>
回答by Darin Dimitrov
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):
以下是正确的重载(在您的示例中,您缺少匿名对象的关闭}
,routeValues
因此您的代码将引发异常):
<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&contact=calgary%2C%20vancouver">
<span>People</span>
</a>
which will successfully invoke the GetByList
controller action passing the two parameters:
这将成功调用GetByList
传递两个参数的控制器操作:
public ActionResult GetByList(string name, string contact)
{
...
}
回答by César León
This works for MVC 5:
这适用于 MVC 5:
<a href="@Url.Action("ActionName", "ControllerName", new { paramName1 = item.paramValue1, paramName2 = item.paramValue2 })" >
Link text
</a>
回答by Bart Calixto
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 :
您可以返回一个名为 HttpValueCollection 的私有集合,即使文档说它是使用 ParseQueryString 实用程序的 NameValueCollection。然后手动添加键,HttpValueCollection 为您进行编码。然后只需手动附加 QueryString :
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>
回答by Developer
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
其中是@Model.ID
参数
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>