2

I have an action method with the following signature,

ActionResult Search(string searchQuery)

This gets called from a partial view on button submit from a form. Problem is, please look at the 2 patterns below. When I submit my search key from my page it uses the following url (suppose search key is tool)

Search/?searchQuery=tool

But then if I click on a tool then,

Search/tool?searchQuery=garden

Now my method is reading tool in the parameter instead of garden (which is expected of course). I presume this is to do with incorrect presentation of items from both the context of the item itself and that of search.

Is there a nice way of resolving this issue? I want to read the query string term and search for it from the main search context i.e. Search/?searchQuery=<term> no matter where I am.

4
  • Can you show your ActionLink or Form code from views? Commented Jul 16, 2013 at 2:56
  • show your code, how you are reading the query string? Commented Jul 16, 2013 at 4:16
  • It should not read tool into searchQuery for either of those, unless you have a routing problem, but we would need to see your routing table for that. Commented Jul 16, 2013 at 4:43
  • It was routing problem as you stated Robert. Cheers Commented Jul 30, 2013 at 6:33

2 Answers 2

2

To get the QueryString, in your controller you should write something like this:

var mystring =Request.QueryString["searchQuery"];

This will get the query string no matter where is placed in your url.

Sign up to request clarification or add additional context in comments.

Comments

1

Rename the input to

ActionResult Search(string searchQuery)

The model binder will then deserialize the query string param to that input value. It will work for both route params and query string params.

1 Comment

Sorry question edited. I just typed it so typed it wrong. It is searchQuery

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.