Search code examples
c#asp.net-mvccontrollerdropdownlistfor

How to get selected value from dropdown list in mvc c#


I have a dropdown list like below. Data is bound using viewbag.

<div class="col-md-4">
    <h2 style="color: #2f6207">dfdfDestination</h2>
    @Html.DropDownList("Destination", new SelectList(ViewBag.Destination, "regCde", "Destination", "Destination"))
</div>

Now I want to send the selected value from the dropdown list to a specific controller. I use jQuery and ajax to do this. It sends the value. But while querying value get null. I have no idea. Is there any other way to do this?

public ActionResult MscHome(string mydestination)
{
    return View();
}

Solution

  • View:

    @using (Html.BeginForm("MscHome", "Home", FormMethod.Get)) 
    {
        <div class="col-md-4">
            <h2 style="color: #2f6207">dfdfDestination</h2>
            @Html.DropDownList("Destination", new SelectList(ViewBag.Destination, "regCde", "Destination", "Destination"))
    
        </div>
        <p>
            <input type="submit" value="Submit" />
        </p>
    
    }
    

    Controller:

    public ActionResult MscHome(string Destination)
    {
        return View();
    }
    

    Notice that you have to specify the action and controller on the Html.BeginForm so the view knows where to perform HttpGet or HttpPost and that the name for your dropdownlist value should be equal to the parameter name of your action result in the controller.