如何创建保留请求中的参数的ActionLink?

[英]How to create an ActionLink that retains the parameters from the request?


I have a view responding to the action /Page/Index/{id} (default ASP.NET MVC routing). The view contains action links that lead to the same action with the same id but with an added parameter in the URL. I've put something like this in place:

我有一个视图响应动作/ Page / Index / {id}(默认的ASP.NET MVC路由)。该视图包含的操作链接导致具有相同ID的相同操作,但在URL中添加了参数。我已经把这样的东西放到了原地:

@Html.ActionLink("SetVar1", "Index", new { var1 = "true" } )
@Html.ActionLink("SetVar2", "Index", new { var2 = "true" } )

As expected, clicking on one of those links redirects me to the same action with the same id and with a parameter.

正如所料,单击其中一个链接会将我重定向到具有相同ID和参数的相同操作。

Page/Index/{id}?var1=true

The user is then presented with the above links again. Of course, if the user clicks again on another action link, only the new parameter is there.

然后再次向用户呈现上述链接。当然,如果用户再次点击另一个操作链接,则只有新参数存在。

Page/Index/{id}?var2=true

What I want instead is to make the action links retain all the previously passed parameters and add a new one (or replace it if it already has a value in the request).

我想要的是让动作链接保留所有先前传递的参数并添加一个新参数(或者如果它在请求中已经有值,则替换它)。

Page/Index/{id}?var1=true&var2=true

How can I do that?

我怎样才能做到这一点?

It is crucial that I don't hardcode all the possible parameter names and I'd also find it very inelegant to do string manipulation to the URL directly. I should also note that the parameters are completely independent of each other.

至关重要的是,我不对所有可能的参数名称进行硬编码,并且我发现直接对URL进行字符串操作非常不优雅。我还应该注意,参数完全相互独立。

1 个解决方案

#1


1  

There are several techniques for doing this, however, none of them is fully automatic, you still have to tell the ActionLink explicitly what you want inside the actionlink.

有几种技术可以做到这一点,但是,它们都不是全自动的,你仍然必须在actionlink中明确地告诉ActionLink你想要什么。

There are a few questions on stackoverflow that describe the same or a similar problem which you could check out:

stackoverflow上有一些问题描述了您可以查看的相同或类似的问题:

智能推荐

注意!

本站翻译的文章,版权归属于本站,未经许可禁止转摘,转摘请注明本文地址:http://www.itdaan.com/blog/2013/05/19/720258c137dcc30d5a5867413c66b42a.html



 
© 2014-2019 ITdaan.com 粤ICP备14056181号  

赞助商广告