Get DropDownList Selected Text and Value in Controller .Net MVC

In a .NET MVC application, dropdown lists are commonly used to allow users to select options from a list. In some cases, it may be necessary to get the selected text and value from a dropdown list in the controller action method. In this blog post, we will explore how to get the selected text and value from a dropdown list in the controller action method using a C# code example.

The Model

Let's create a class called Employee with properties Id and Name to simulate our data:
public class Employee
{
    public int Id { get; set; }
    public string Name { get; set; }
}
The Controller

In our controller, we can create a list of Employee objects and use it to bind the data to our dropdown list. Here is an example:
public ActionResult Index()
{
    List<Employee> employeesList = new List<Employee>
    {
        new Employee {Id = 1, Name = "John Doe"},
        new Employee {Id = 2, Name = "Jane Smith"},
        new Employee {Id = 3, Name = "Bob Johnson"}
    };

    ViewBag.Employees = new SelectList(employeesList, "Id", "Name");

    return View();
}

[HttpPost]
public ActionResult Index(FormCollection form)
{
    int selectedId = Convert.ToInt32(form["Employees"]);
    string selectedText = ((SelectList) ViewBag.Employees).FirstOrDefault(c => c.Value == selectedId.ToString())?.Text;

    // Do something with selectedId and selectedText

    return View();
}
In this example, we create a list of Employee objects and use the SelectList constructor that takes three parameters. The first parameter is the list of Employee objects, the second parameter is the name of the property to use for the value (Id in this case), and the third parameter is the name of the property to use for the text (Name in this case).

We then store this SelectList object in the ViewBag using the key "Employees". In our POST action method, we retrieve the selected value using the "Employees" key from the FormCollection object. We then convert the selected value to an integer and use it to retrieve the corresponding Employee object from the SelectList stored in the ViewBag. Finally, we retrieve the text value of the selected item and use it in our application.

The View

In our view, we can create a dropdown list using the Html.DropDownList helper method. Here is an example:
@Html.DropDownList("Employees", (SelectList) ViewBag.Employees, "-- Select Employee --", new { @class = "form-control" })
In this example, we are creating a dropdown list with the name "Employees" and using the ViewBag.Employees object to populate the options. The "-- Select Employee --" string is used as the default option text. The new { @class = "form-control" } object is used to apply the Bootstrap form-control class to the dropdown list.

Conclusion

Getting the selected text and value from a dropdown list in the controller action method is a straightforward process in a .NET MVC application. By using a class to represent our data and the appropriate syntax, we can easily bind data to our dropdown list and retrieve the selected text and value. With this knowledge, you can create more dynamic and interactive applications that better meet the needs of your users.
 
About Us | Terms of Use | Privacy Policy | Disclaimer | Contact Us Copyright © 2012-2024 CodingFusion
50+ C# Programs for beginners to practice