6

I have the following form

<form name="SearchForm" method="post" id="SearchForm" action="/Search/">

And the following button

<input type="button" onclick="javascript:document.SearchForm.submit();" class="btn-leftsearch">

On clicking this button, the form submits and calls this method

[HttpPost]
public ActionResult Index(string querystring)
{
   return View();
}

Of course querystring is null. I want to pass querystring or preferably something else representing the fields in the form to the controller. I have tried playing with the action attribute in the form tag. I have tried to add the data to the onclick method in the button. Nothing is working. All I want to do is pass some data like this

Search?pri=all&amenity=pool etc

In the controller I would have something like

[HttpPost]
public ActionResult Index(string pri, List<string> amenities)
{
   ...
}

Can someone tell me how I can pass this data to the view?

1 Answer 1

11

I would like to suggest you that you can use the following code snip to resolve you problem.

[AcceptVerbs(HttpVerbs.Post)]
public ActionResult Index(FormCollection collection)
{
     string valueFromNameTextBox = collection["name"];
}

on the collection please put the name of the search text box. You wil get the actual entered value. You can index into this collection with the names of all the inputs on the form.

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

1 Comment

I see but I have some other data that I want to pass too that are not in form fields. How do I pass those?

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.