Click here to Skip to main content
15,885,309 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 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 8:57am
v2

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:
C#
$("#sltCourse").change(function () {
            selectedCourseName = $("#sltCourse").val().trim();

..............
..............
                getFacultyTable();
                getStudentTable();
..............
..............
               
            }
        });
 
Share this answer
 
Comments
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    
Sure...it is:
function getFacultyTable() {
$.ajax({
// Get Faculty PartialView
url: "/Home/FacultiesToPVDemo",
type: 'Get',
data: { courseName: selectedCourseName },
success: function (data) {
$("#facultyDetailTable").empty().append(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

C#
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
C#
@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
 
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