ASP.NET MVC Passing form values to action method

11,057

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.

Share:
11,057
Sachin Kainth
Author by

Sachin Kainth

I am a senior C#, .NET and AngularJS developer living in London.

Updated on July 04, 2022

Comments

  • Sachin Kainth
    Sachin Kainth almost 2 years

    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?