Click here to Skip to main content
15,942,934 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:

have a dropdown in my view, couple of other controls and a input button to save the data.
On saving I have ActionReult Save ()
which needs the value I selected in the dropdownlist.
How can I get the selected value of the dropdownlist on my controller Actionresult Save()

var docTypes = Model.DocumentTypes.Select(x => new SelectListItem { Text = x.Name, Value = x.Id.ToString() }).AsEnumerable();
@Html.DropDownList("SelectedDocumentType", docTypes, "--select--", new { @class = "ddSelect" })

public IEnumerable<doctypeentity> DocumentTypes { get; set; }

public ActionResult Save()
int DocumentType = ????// I have to assign the selected value of the dropdownlist here
Updated 22-Oct-14 5:18am

1 solution

If using @Html.DropDownList with a name "SelectedDocumentType", use FormCollection as it will bring all the values from view having name i.e. public ActionResult Save(FormCollection fc) and then fc["SelectedDocumentType"] should give you value.

Also, if you have a Model with property Document type, then I prefer using @Html.DropdownListFor(Model.DocumentType). Much better approach.

Please like if it helps. Thanks.
Share this answer

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900