Click here to Skip to main content
15,936,709 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 DocumentTypes { get; set; }

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

Please refer to the article and attached code with that:
Multiple Models in a View in ASP.NET MVC 4 / MVC 5[^]

We are using below code to get selected course and then calling a function which make AJAX call to call partial view:
$("#sltCourse").change(function () {
            selectedCourseName = $("#sltCourse").val().trim();

Share this answer
nilufar21683 23-Oct-14 12:13pm    
Hello Snesh,
Can you please send across the code snippet where in you are sending the selected value using Ajax ?
Snesh Prajapati 23-Oct-14 12:43pm is:
function getFacultyTable() {
// Get Faculty PartialView
url: "/Home/FacultiesToPVDemo",
type: 'Get',
data: { courseName: selectedCourseName },
success: function (data) {
error: function () {
alert("something seems wrong");
Snesh Prajapati 23-Oct-14 12:44pm    
You can see the line -> data: { courseName: selectedCourseName },
Create one more Model class like below

public class NewModelClass{

//create a property  selected Id
public int SelectedId{get;set;}
//Move the DocumentTypes to this new class
public IEnumerable DocumentTypes { get; set; }


//Pass this class as Parameter in Save ActionResult
public ActionResult Save(NewModelClass objNewModelClass){
//here you will get selectedId
var selectedId=objNewModelClass.SelectedId;


Change your dropDown like below by surrouding Html.BeginForm in View
@model NewModelClass
    ViewBag.Title = "Index";

@using (Html.BeginForm("YourControllerName","Save",FormMethod.Post))    
@Html.DropDownListFor(model => model.SelectedId,DocumentTypes, "--select--", new { @class = "ddSelect" })

<input type="submit" value="Click"/>

Hope this helps
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