Mark Sandman
Mark Sandman

Reputation: 3333

Add a query string to a HtmlHelper in ASP.NET MVC

I have a view with the two HtmlHelpers that produce links, something like this

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

Now I wish to add a querystring to Link B so when it points to the following URL http://localhost:55556/HomeController/?Sort=LinkB

I want both links to point to the same controller so I can then detect if the queryString is present then point the appropriate link to a different view, some thing like...

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

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

Thanks for the help.

Upvotes: 3

Views: 1178

Answers (2)

Lee Gunn
Lee Gunn

Reputation: 8656

Is there a reason you can't use:

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

Upvotes: 2

Tejs
Tejs

Reputation: 41236

You simply provide the query string parameters in a dictionary. The following question on SO might interest you: QueryString parameters.

In your situation it would simply be

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

Upvotes: 1

Related Questions