Reputation: 13818
Can someone help me with getting values from a dropdownlist in asp.net mvc?
I can get values from textboxes,etc...but,how do I get these 2 things...
Thanks
Upvotes: 2
Views: 22108
Reputation: 4660
I think you need to rethink this. The values for the dropdown should come from the controller and sent to the view for display in the dropdown to allow the user to select. Then the page form sends the selected value back to the controller. Data should always be on the server side and the view is just for display.
Upvotes: 1
Reputation: 24522
You can get the selected value from a drop down list the same way as you do for text boxes.
Using default model binding
[AcceptVerbs(HttpVerbs.Post)]
public ActionResult GetValueExample(string MyList) {
//MyList will contain the selected value
//...
}
or from a FormCollection
[AcceptVerbs(HttpVerbs.Post)]
public ActionResult GetValueExample(FormCollection form) {
string val = form["MyList"];
//...
}
or from the request
[AcceptVerbs(HttpVerbs.Post)]
public ActionResult GetValueExample(string MyList) {
string val = Request.Form["MyList"]; //or
val = Request["MyList"];
//...
}
Where your drop down list is named "MyList".
<%= Html.DropDownList("MyList", MyItems) %>
or straight HTML
<select name="MyList">
<option value="1">Item 1</option>
<option value="2">Item 2</option>
</select>
The browser will only submit the selected value from the drop down list and not all the other values. To get the list of all the other items you should invoke the code that populated the list in the first place (assuming you used Html.DropDownList()).
Update
[AcceptVerbs(Http.Get)]
public ActionResult GetValueExample() {
ViewData["MyItems"] = GetSelectList();
return View();
}
[AcceptVerbs(Http.Get)]
public ActionResult GetValueExample(string MyList) {
//MyList contains the selected value
SelectList list = GetSelectList(); //list will contain the original list of items
//...
}
private SelectList GetSelectList() {
Dictionary<string, string> list = new Dictionary<string, string>();
list.Add("Item 1", "1");
list.Add("Item 2", "2");
list.Add("Item 3", "3");
return new SelectList(list, "value", "key");
}
//...
<%= Html.DropDownList("MyList", ViewData["MyItems"] as SelectList) %>
Upvotes: 15
Reputation: 6802
Well it's hard to answer correctly since you've given so little information, but in general you get the selected value in the post method of the Controller.
Something like this might explain it better:
Consider having this dropdownlist:
//instantiate the dropdownlist in the controller method.
public ActionResult Create() {
List<string> items = new List<string>() {"first", "second", "third"};
SelectList SomeSelectItems = new SelectList(items);
ViewData["list"] = SomeSelectItems;
return View();
}
<%= Html.DropDownList("DDL", (SelectList)ViewData["list"]) %>
In your controller you would get the value of the dropdownlist like this:
[AcceptVerbs(HttpVerbs.Post)]
public ActionResult Create(string DDL)
{
string theValue = DDL;
return View();
}
To get all the values from the dropdownlist would be the same as putting them into the selectlist in the first place. I will assume you have a method that is called to fill the dropdownlist with items. Simply call this method from within the controller and handle the values appropriately.
Upvotes: 1