如何創建保留請求中的參數的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上有一些問題描述了您可以查看的相同或類似的問題:


注意!

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



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