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:
Hi,

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()

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

Model:
-----
C#
public IEnumerable<doctypeentity> DocumentTypes { get; set; }

Controller:
-----------
C#
[HttpParamAction]
[HttpPost]
[ValidateInput(false)]
public ActionResult Save()
{
int DocumentType = ????// I have to assign the selected value of the dropdownlist here
}
Posted
Updated 22-Oct-14 5:18am
v2

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
 
v2

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