Hi i need some help on MVC as new in this area. Here is my code for Dropdown list and I just want to get the selected values by user in a controller so that I can pass that value to a table in db. Here is the code from .cshtml
@Html.DropDownList("LogOffTime", new List<SelectListItem>
{ new SelectListItem{ Text="One", Value = "30000" }, new SelectListItem{ Text="Two", Value = "60000" }, new SelectListItem{ Text="Three", Value = "180000" }
}, "Select Time") Controller:
public ActionResult Index() {How to get the value from drop down list}I just need to know how to access the value i.e. the 30000/60000 etc. in controller. Also need to check that no value will be passed if the user does not select anything. Also correct me pls. if anything wrong in my code. Thanks!
35 Answers
I have this example from what I am working right know. I hope it can help
View or DropDownList:
@using(Html.BeginForm("getSelectedValue", "Home", FormMethod.Post, new { enctype = "multipart/form-data" }))
{ @Html.DropDownList("LogOffTime", new List<SelectListItem> { new SelectListItem{ Text="One", Value = "30000" }, new SelectListItem{ Text="Two", Value = "60000" }, new SelectListItem{ Text="Three", Value = "180000" } }, "Select Time") <input type="submit" value="Submit" />
}HomeController:
[HttpPost]
public ActionResult getSelectedValue()
{ var selectedValue = Request.Form["LogOffTime"].ToString(); //this will get selected value
} 3 First of all you need a model.. Lets say you have Id column in your model..
public ActionResult Index() { ViewBag.MyList= new SelectList(db.MyTable.ToList(), "Id", "Name"); return View(); }In Razor
@Html.DropDownListFor(m => m.Id, ViewBag.MyList as SelectList, new { @class = "form-control" })POST Controller Method
[HttpPost]
[ValidateAntiForgeryToken]
public async Task<ActionResult> Index(MyModel model)
{ int getmyid=model.Id;
} Create a model
public class dropdown {
public int value {get;set;}
}You need to add using around your dropdown list like this, Add a submit button which will do a post to the controller-
@using(Html.BeginForm("Index", "Home", FormMethod.Post, new { enctype = "multipart/form-data" })) { @Html.DropDownList(m=>m.value,"LogOffTime", new List<SelectListItem> { new SelectListItem{ Text="One", Value = "30000" }, new SelectListItem{ Text="Two", Value = "60000" }, new SelectListItem{ Text="Three", Value = "180000" } }, "Select Time") <input type="submit" value="Submit" /> }In your controller
public ActionResult AddItem(dropdown value) // you will get the value here
{ // do something
} 2 Its very simple you just need to understand only one thing ,For dropdownlist control, view form will post only the selected value along with name of dropdownlist control as a key-value pair to the controller method. So you can simply write like below to get value
View Code:
@Html.DropDownList("LogOffTime", new List<SelectListItem>
{ new SelectListItem{ Text="One", Value = "30000" }, new SelectListItem{ Text="Two", Value = "60000" }, new SelectListItem{ Text="Three", Value = "180000" }
}, "Select Time") Controller Code:
public ActionResult Index( string LogOffTime) //LogOffTime is name of dropdownlist controll
{ //some logic
}Note: You should put the ddcontrol inside your form tag, and post the data using the submit or you can also do the same thing with change event of jquery.
Hope the above information was helpful
Thanks
Karthik
SelectLists are one of the more challenging concepts for people (me) to grasp. Part of the confusion is that when we set the "Text", we intuitively think that "Value" is an number (int or "Id"). It's not. "Value" is passed from the view to the controller as a string. Then, when the form is submitted to the controller, you'll want to parse the "Value" and convert the value to an int. See below:
Your viewmodel should have a property of type 'string' that will hold the selected value.
ViewModel
public class dropdown {
public string selectedvalue {get;set;}
}View
@using(Html.BeginForm("Index", "Home", FormMethod.Post, new { enctype = "multipart/form-data" })) { @Html.DropDownListFor(m=>m.selectedvalue, new List<SelectListItem> { new SelectListItem{ Text="One", Value = "30000" }, new SelectListItem{ Text="Two", Value = "60000" }, new SelectListItem{ Text="Three", Value = "180000" } }, "Select Time") <input type="submit" value="Submit" /> }Controller
public ActionResult Index(string selectedValue)
{ int value = 0; bool valueIsInt = Int32.TryParse(selectedValue, out value); //this returns true if selectedValue can be converted to int and outputs the int as "value" if (valueIsInt)
{
selectedvalue = value // if value is an int, set it to selectedvalue
}
//otherwise do something else
}I know this is a little late, but hopefully this can help someone struggling with selectlists as I was.