如何在MVC中传递ActionLink中的查询字符串参数

时间:2021-07-30 13:13:05

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

4 个解决方案

#1


95  

4th parameter of Html.ActionLink can have any number of properties:

4参数的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:

因此,如果您有标准的路由{控制器}/{动作}/{id},您将获得URL:

test/Edit/[id]?data=[name] 

from the above code.

从上面的代码。

#2


2  

I know this is kind of old question but.

我知道这是个老问题。

In case the below code doesn't generate the <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).

我将建议检查您的操作是否至少有一个[路由]属性(我使用[路由("/")])。

Hope it helps.

希望它可以帮助。

#3


1  

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

通过上面的代码,您将获得如下url(假设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) :

通过上面的代码,您将获得如下url(假设Id=1, Name=India):

/Home/Delete/1?Name=India

#4


0  

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把我的头撞到墙上。当您只想将它路由到直接的路由调用时,但是当您希望在末尾添加一个简单的querystring时,绝对拒绝合作时,这种方法非常有效。

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。在锚标记内的动作。

<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路由中为querystring正常工作。

#1


95  

4th parameter of Html.ActionLink can have any number of properties:

4参数的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:

因此,如果您有标准的路由{控制器}/{动作}/{id},您将获得URL:

test/Edit/[id]?data=[name] 

from the above code.

从上面的代码。

#2


2  

I know this is kind of old question but.

我知道这是个老问题。

In case the below code doesn't generate the <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).

我将建议检查您的操作是否至少有一个[路由]属性(我使用[路由("/")])。

Hope it helps.

希望它可以帮助。

#3


1  

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

通过上面的代码,您将获得如下url(假设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) :

通过上面的代码,您将获得如下url(假设Id=1, Name=India):

/Home/Delete/1?Name=India

#4


0  

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把我的头撞到墙上。当您只想将它路由到直接的路由调用时,但是当您希望在末尾添加一个简单的querystring时,绝对拒绝合作时,这种方法非常有效。

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。在锚标记内的动作。

<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路由中为querystring正常工作。