pass selected dropdown list value from View to Controller
asp.net-mvc, c#
Solution
You could just use a string in your model,
public string emails { get; set; };
then Use a `DropDownListFor` on your view,
@Html.DropDownListFor(x => x.emails, Model.GetEmails())
and get the value directly from the model on the `POST`.
[HttpPost]
public ActionResult Send(OverviewViewModel overviewModel)
{
//here is the value from the drop down
var theEmail = overviewModel.emails;
}
Problem
I want to pass a parameter (string) to my Controller from my View. The value should be that which is selected from a dropdown list in the view. The method is called after a button-click, but in the Controller, the parameter is always null. In the View: ``` @using (Html.BeginForm("Send", "Overview", FormMethod.Get)) { <p>Email: @Html.DropDownList("Emails", Model.GetEmails()) <input type="submit" value="Send" /> </p> } ``` In the Controller: ``` public ActionResult Send(string email) { // email is always null! return null; } ``` In the Overview model: ``` private List<SelectListItem> emails; ... public Overview() { emails = new List<SelectListItem>(); emails.Add(new SelectListItem() { Text = "a@b.com", Value = "a@b.com", Selected = true }); emails.Add(new SelectListItem() { Text = "b@c.com", Value = "b@c.com", Selected = false }); emails.Add(new SelectListItem() { Text = "c@d.com", Value = "c@d.com", Selected = false }); } ... public List<SelectListItem> GetEmails() { return emails; } ``` Could you please help me get this work? What am I missing? (Btw, never mind the Controller returning null for now please :)).