How to send data from view to controller on button click

watbywbarif picture watbywbarif · Jun 21, 2013 · Viewed 10.7k times · Source

How can I invoke controller action and send which values are selected in drop down lists in time when button was clicked? Here is example how my .cshtml looks like. This is just example, generally I need to collect much data from current view in time when button was clicked.

<body>
    <div>
        @Html.DropDownList("Name")
        <br />
        @Html.DropDownList("Age")
        <br />
        @Html.DropDownList("Gender")
        <br />
        @using (Html.BeginForm("FindPerson", "MyController", FormMethod.Post))
        {
            <input type="submit" value="Find" />
        }
    </div>
</body>

Answer

drch picture drch · Jun 21, 2013

In order for the data to be submitted to the controller, the inputs must appear within the <form> tag.

For example:

<body>
    <div>
        @using (Html.BeginForm("FindPerson", "MyController", FormMethod.Post))
        {

            @Html.DropDownList("Name")
            <br />
            @Html.DropDownList("Age")
            <br />
            @Html.DropDownList("Gender")
            <br />
            <input type="submit" value="Find" />
        }
    </div>
</body>