Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ASP.NET MVC get dropdown list value

In ASP.NET MVC, how can I get a selected dropdown list value from a posted form?

like image 655
Smallville Avatar asked May 14 '09 11:05

Smallville


People also ask

How do I get the selected value of dropdown?

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.

How do I pass a dropdown value to a controller?

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.


2 Answers

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.

like image 191
Garry Shutler Avatar answered Oct 05 '22 23:10

Garry Shutler


public class MyController
{
    public ActionResult MyAction (FormCollection form)
    {
        string value = form["DropDownListName"];
    }
}
like image 21
User Avatar answered Oct 05 '22 23:10

User