Html.DropdownListFor in .NET MVC with Code Examples

Html.DropdownListFor is a helper method in .NET MVC that allows developers to easily create dropdown lists in their web applications. In this blog post, we'll explore how to use Html.DropdownListFor to create dropdown lists and customize their behavior with different features like CSS classes, HTML attributes, required attribute, default values, and read-only attributes.

Basic usage of Html.DropdownListFor

The basic usage of Html.DropdownListFor involves passing a list of SelectListItem objects to the method, along with a lambda expression that specifies the property that the selected value should be bound to in the model.

Here's an example of how to use Html.DropdownListFor:

Controller

public class HomeController : Controller
{
    public IActionResult Index()
    {
        var countries = new List<SelectListItem>
        {
            new SelectListItem { Text = "United States", Value = "us" },
            new SelectListItem { Text = "Canada", Value = "can" },
            new SelectListItem { Text = "United Kingdom", Value = "uk" }
        };

        var model = new MyViewModel { Countries = countries };

        return View(model);
    }
}

View

@model MyViewModel

@using (Html.BeginForm())
{
    @Html.LabelFor(model => model.Country)
    @Html.DropdownListFor(model => model.Country, Model.Countries, "Select a country")
    
}

In this example, we're creating a dropdown list for the Country property in our model. The countries variable contains a list of SelectListItem objects, which will be used to populate the dropdown list.


Adding a CSS class

To add a CSS class to the dropdown list, we can pass an anonymous object as a third parameter to the DropdownListFor method. This anonymous object can contain any HTML attributes we want to add to the generated select element.

@Html.DropdownListFor(model => model.Country, countries, new { @class = "form-control" })

In this example, we're adding the form-control class to the generated select element, which will apply Bootstrap styling to the dropdown list.


Adding HTML attributes

We can also add HTML attributes to the select element by including them in the anonymous object passed as the third parameter to the DropdownListFor method. Here's an example:

@Html.DropdownListFor(model => model.Country, countries, new { id = "country-dropdown", tabindex = 1 })

In this example, we're adding id and tabindex attributes to the generated select element.


Adding the Required attribute

If we want to make the dropdown list required, we can add the required attribute to the generated select element by including it in the anonymous object passed as the third parameter to the DropdownListFor method. Here's an example:

@Html.DropdownListFor(model => model.Country, countries, new { @class = "form-control", required = "required" })

In this example, we're adding the required attribute to the generated select element, which will make it required to be selected before submitting the form.


Setting a Default Value

We can set a default value for the dropdown list by including a selected attribute in the SelectListItem object for the default value. Here's an example:

@{
    var countries = new List<SelectListItem>
    {
        new SelectListItem { Text = "United States", Value = "us", Selected = true },
        new SelectListItem { Text = "Canada", Value = "can" },
        new SelectListItem { Text = "United Kingdom", Value = "uk" }
    };
}

@Html.DropdownListFor(model => model.Country, countries)

In this example, the "United States" option will be selected by default when the dropdown list is rendered.


Making the dropdown list read-only

If we want to make the dropdown list read-only, we can add the disabled attribute to the generated select element by including it in the anonymous object passed as the third parameter to the DropdownListFor method.

@Html.DropdownListFor(model => model.Country, countries, new { @class = "form-control", disabled = "disabled" })

In this example, we're adding the disabled attribute to the generated select element, which will make it read-only.

In this blog post, we've explored how to use Html.DropdownListFor in .NET MVC to create dropdown lists and customize their behavior with different features like CSS classes, HTML attributes, required attribute, default values, and read-only attributes. By using these features, developers can create more interactive and user-friendly web applications.

 
Best quality Asp .Net Ajax Control Toolkit tutorials.

Give your valuable comments.

Name
Email
Comment
7 + 6 =
 

About Us | Terms of Use | Privacy Policy | Disclaimer | Contact Us Copyright © 2012-2024 CodingFusion
50+ C# Programs for beginners to practice