Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ASP.NET MVC Passing form values to action method

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?

like image 501
Sachin Kainth Avatar asked Sep 04 '12 12:09

Sachin Kainth


1 Answers

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.

like image 189
Rajesh Kumawat Avatar answered Oct 11 '22 16:10

Rajesh Kumawat