In ASP.NET MVC, how can I get a selected dropdown list value from a posted form?
Method 1: Using the value property: The value of the selected element can be found by using the value property on the selected element that defines the list. This property returns a string representing the value attribute of the <option> element in the list. If no option is selected then nothing will be returned.
If you want to pass the selected value of your dropdown list into your controller, just add a string parameter with the name drpFields (same as the first parameter of your DropDownList method) into your temp action method.
public class MyController
{
public ActionResult MyAction(string DropDownListName)
{
}
}
This will do the line of code in MasterMind's answer for you. Which method you want to use depends on your situation. Either is fine in my opinion.
If all your possible selected values are numbers you can also do this:
public class MyController
{
public ActionResult MyAction(int DropDownListName)
{
}
}
It will then convert the string of the selected value into an integer for you.
public class MyController
{
public ActionResult MyAction (FormCollection form)
{
string value = form["DropDownListName"];
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With