简体   繁体   中英

ASP.NET MVC 4 : How to get selected dropdown values in controller action?

I have a view as given below. I have two submit buttons; one for find new car and another for find used car . How can I can get the value of selected dropdown in controller ?

And how can I find out which of the 2 buttons have been pressed by the user?

@{
        ViewBag.Title = "Home Page";
    }
    @using (Html.BeginForm("Home", "home"))
    {

        <table>
            <tr>
                <td>
                    <h2>
                        Find New car</h2>
                    <table>
                        <tr>
                            <td>
                                <select id="ddlBrand" name="ddlbrand" style="width: 200px;">
                                    <option value="0">---Select Brand---</option>
                                    <option value="1">---b1---</option>
                                </select>
                            </td>
                        </tr>
                        <tr>
                            <td>
                                <select id="ddlModel" name="ddlModel" style="width: 200px;">
                                    <option value="0">---Select Model---</option>
                                </select>
                            </td>
                        </tr>
                        <tr>
                            <td>
                                <select id="ddlFuelType" name="ddlFuelType" style="width: 200px;">
                                    <option value="0">---Select Fuel Type---</option>
                                </select>
                            </td>
                        </tr>
                        <tr>
                            <td>
                                <select id="ddlBudget" name="ddlBudget" style="width: 200px;">
                                    <option value="0">---Select Your Budget---</option>
                                </select>
                            </td>
                        </tr>
                        <tr>
                            <td>
                                <input type="button" name="FindNewCar" value="GO"  />
                            </td>
                        </tr>
                    </table>
                </td>
                <td>
                    <h2>
                        Find used car</h2>
                    <table>
                        <tr>
                            <td>
                                <table>
                                    <tr>
                                        <td>
                                            <select id="ddlBrand1" name="ddlbrand" style="width: 200px;">
                                                <option value="0">---Select Brand---</option>
                                            </select>
                                        </td>
                                    </tr>
                                    <tr>
                                        <td>
                                            <select id="ddlModel2" name="ddlModel" style="width: 200px;">
                                                <option value="0">---Select Model---</option>
                                            </select>
                                        </td>
                                    </tr>
                                    <tr>
                                        <td>
                                            <select id="ddlFuelType2" name="ddlFuelType" style="width: 200px;">
                                                <option value="0">---Select Fuel Type---</option>
                                            </select>
                                        </td>
                                    </tr>
                                    <tr>
                                        <td>
                                            <select id="ddlCity" name="ddlCity" style="width: 200px;">
                                                <option value="0">---Select City---</option>
                                            </select>
                                        </td>
                                    </tr>
                                    <tr>
                                        <td>
                                            <input type="button" name="FindNewCar" value="GO" />
                                        </td>
                                    </tr>
                                </table>
                            </td>
                            <td>
                                <table>
                                    <tr>
                                        <td>
                                            <h4>
                                                Sell Car</h4>
                                            <br />
                                            Sell your car faster, at right price ...<a href="#"> Sell Car»</a>
                                        </td>
                                    </tr>
                                </table>
                            </td>
                        </tr>
                    </table>
                </td>
                <td>
                    aaaaaaaaaaa
                </td>
            </tr>
        </table>

    }

You should setup two action methods inside of your controller and use the Html.ActionLink method to help create your form.

Example:

public ActionResult FindNewCar(string ddlbrand, string ddlModel, string ddlFuelType, string ddlBudget)
{
     // Do something here.
}

public ActionResult FindUsedCar(string ddlbrand2, string ddlModel2, string ddlFuelType2, ddlCity)
{
     // Do something here.
}

View

@using (Html.BeginForm("Home", "home"))
{
     @Html.ActionLink(...)
}

EDIT

See changes above

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM