0

I want to use BegingForm with Get method and this is what I do

@using (Html.BeginForm("Search","Home",FormMethod.Get))
{ 
   //My input elements

}

public class HomeController : Controller
{
    public ActionResult Search(string queryString)
    {
    }

}

but query string always comes back as null. I think, I need to do something with route but no luck

routes.MapRoute(
            "SearchRoute", // Route name
            "Home/Search{queryString}", // URL with parameters
            new { controller = "Home", action = "Search", filter = UrlParameter.Optional } // Parameter defaults
            );

Obviously, the coming url to the server is something like

Home/Search?query="blah"&query2="blah"&query3="blah"

What am I doing wrong? What is the correct way to get the query parameters in my controller when I want to use get with beginform?

Also, what if the content of my BeginForm can change and so the query string parameter names could be different depending on the rendered page but I want one Search method that analyze the query string and do the right thing?

Also, is a way for them to query parameters to come in a dictionary?

2 Answers 2

1

Obviously, the coming url to the server is something like Home/Search?query="blah"&query2="blah"&query3="blah"

That's how HTML <form> with method GET works and this has nothing to do with ASP.NET MVC, it's plain HTML. You can't do much about it other than having your controller action look like this:

public ActionResult Search(SearchViewModel model)
{
    ...
}

Where SearchViewModel will contain properties for each input field on this form. Also you don't need this SearchRoute as it won't work that way.

This being said you could probably use javascript in order to subscribe for the onsubmit event of the form, cancel the default submission (which exhibits the behavior you are observing currently), manually fetch all the values inside your form and then manually generate the url you want and redirect to it using window.location.href = '....';. I am mentioning this only for completeness but absolutely not as something that I recommend or that you should ever do.

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

2 Comments

Are you saying mvc framework can automatically deserialize my query string to SearchViewModel? Do I need to decorate SearchViewModel in anyway to make this happen? Would you please refer me to any doc on how to map the query string to SearchViewModel?
@Mike Dotnet, yes, that's what the default model binder does. You don't have to decorate anything. You just must ensure that the name of the properties on this model correspond to the names of the input fields inside your HTML <form>. But of course if you use strongly typed helpers such as Html.TextBoxFor and pass a lambda expression to generate those input fields the framework will take care of properly naming them so tat you don't have to worry about it.
0

If you want to get the items from the query string, just use the "Request" object from the ControllerBase:

    public ActionResult Search()
    {
         var queries = new List<string>();
         foreach (var parameter in Request.QueryString)
         {
             queries.Add(parameter.ToString());
         }
         //Do Stuff with the query parameters...
         return View("Index");
    }

And "Request.QueryString" is a dictionary just as you wanted :)

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.