3

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.

2 Answers 2

2

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>
Sign up to request clarification or add additional context in comments.

2 Comments

Hi there, this seems to work, using the above gave me the answer provided by Tejs produced the following URL from the HtmlHelper : localhost:55556/?Length=18 Why I know not?
The other example is using the wrong overload and therefore the third parameter (which is the controller name) is being used as the object for the route values. The object is a string. A string has a Length property. The length of the controller name you were using must have been 18 characters wide.
1

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" }) %>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.