簡體   English   中英

將查詢字符串添加到ASP.NET MVC中的HtmlHelper

[英]Add a query string to a HtmlHelper in ASP.NET MVC

我有兩個生成鏈接的HtmlHelper視圖,就像這樣

<li><%:Html.ActionLink("Link A", "Index", "HomeController")%></li>
<li><%:Html.ActionLink("Link B", "Index", "HomeController"})%></li>

現在我希望為鏈接B添加一個查詢字符串,以便它指向以下URL http:// localhost:55556 / HomeController /?Sort = LinkB

我希望兩個鏈接指向同一個控制器,以便我可以檢測是否存在queryString然后將相應的鏈接指向不同的視圖,有些事情如...

[AcceptVerbs(HttpVerbs.Get)]
        public ActionResult Index()
        {
            var linkChoice = Request.QueryString["Sort"];

            if (linkChoice == "LinkB")
            {
                return View("ViewB");
            }
            else
            {
               return View("ViewA");
            }
        }

謝謝您的幫助。

有沒有理由你不能使用:

<li><%:Html.ActionLink("Link A", "Index", "HomeController", new { Sort = "LinkA" }, null)%></li>
<li><%:Html.ActionLink("Link B", "Index", "HomeController", new { Sort = "LinkB" }, null)%></li>

您只需在字典中提供查詢字符串參數。 關於SO的以下問題可能會讓您感興趣: QueryString參數

在你的情況下,它只是

<%= Html.ActionLink("Name", "Index", "Controller", new { Sort = "LinkB" }) %>

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM