asp.net-mvc 如何在MVC中的ActionLink中传递查询字符串参数
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3100923/
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
How to pass query string parameter in ActionLink in MVC
提问by Ashwani K
I am having following action link:
我有以下操作链接:
<%= Html.ActionLink("Check this", "Edit", "test",
new { id = id }, new { style = "display:block" })%>
How do I include data=name
as query string. Some thing like this:
如何包含data=name
作为查询字符串。像这样的事情:
link?data=name
回答by PanJanek
4th parameter of Html.ActionLink
can have any number of properties:
的第四个参数Html.ActionLink
可以有任意数量的属性:
<%= Html.ActionLink("Check this", "Edit", "test",
new { id = id, data=name }, new { style = "display:block" })%>
These properties are inserted into URL based on routing, but if the property name cannot be matched into any route it is added as URL GET parameter.
这些属性根据路由插入到 URL 中,但如果属性名称无法匹配到任何路由,则将其添加为 URL GET 参数。
So if you have standard route {controller}/{action}/{id}
, you will get the URL:
因此,如果您有标准 route {controller}/{action}/{id}
,您将获得 URL:
test/Edit/[id]?data=[name]
from the above code.
从上面的代码。
回答by Oleg Karasik
I know this is kind of old question but.
我知道这是一个老问题,但是。
In case the below code doesn't generate the <a href="/?param=value" />
.
如果下面的代码没有生成<a href="/?param=value" />
.
<%= Html.ActionLink("Text", "Action", "Controller", new { param=value }, null)%>
I would advice checking whether you action has at least one [Route]
attribute (I used [Route("/")]
for example).
我建议检查您的操作是否至少具有一个[Route]
属性([Route("/")]
例如我使用过)。
Hope it helps.
希望能帮助到你。
回答by Sanam Tiwari
Pass Query String By this way
通过这种方式传递查询字符串
@Html.ActionLink("Delete Record", "Home", "Delete", new { id=Id},null)
By above code you will get the url like(Suppose Id=1): /Home/Delete/1
通过上面的代码,您将获得如下网址(假设 Id=1): /Home/Delete/1
and if you want to add more parameters to query string then:
如果您想向查询字符串添加更多参数,则:
@Html.ActionLink("Delete Record", "Home", "Delete", new { id=Id, Name=name},null)
By above code you will get the url like(Suppose Id=1 and Name=India) :
通过上面的代码,您将获得像(Suppose Id=1 and Name=India) 这样的网址:
/Home/Delete/1?Name=India
回答by Harry Binnendyk
I got tired of banging my head against a wall with the html.actionlink. Works great when you just want to route it against straightforward routing calls, but absolutely refuses to cooperate when you want to add a simple querystring at the end.
我厌倦了用 html.actionlink 撞墙。当您只想针对简单的路由调用对其进行路由时效果很好,但是当您想在最后添加一个简单的查询字符串时绝对拒绝合作。
I don't an ID at then end, I want to be able to add some kind of actual Querystring with the "?".
我最后没有 ID,我希望能够添加某种带有“?”的实际查询字符串。
So anywhere I needed a Querystring I switched to using the url.action inside the anchor tag.
所以在任何需要查询字符串的地方,我切换到使用锚标记内的 url.action。
<a href='@url.action("Action","route")?Parameter=Value' >Text for Link Name</a>
At least it works and I can stop getting headaches over something that should have been a very simple task. Someone needs to get their heads out of their butts and make the ActionLink work properly for Querystrings in the MVC routing.
至少它有效,我可以不再为本应是一项非常简单的任务而头疼。有人需要把他们的头从他们的屁股上拿出来,让 ActionLink 为 MVC 路由中的 Querystrings 正常工作。