Asp net mvc html dropdownlist selectedvalue

ASP.NET is a popular language used for developing web applications. One common requirement in web is to a dropdown list and set the selected value dynamically. In this article, we will explore how to achieve this using ASP.NET MVC and HTML.

To begin with, let's create a simple ASP.NET MVC . Open Visual Studio and select “Create a new project.” Choose the ASP.NET Web Application template and name your project. Select MVC as the project template and click on “Create.”

Once the project is created, open the “HomeController.cs” file located in the “Controllers” folder. Inside the HomeController class, add a new action method called “Index” as shown below:


public ActionResult Index()
{
    ViewBag.Countries = new SelectList(new[]
    {
        new { Id = 1, Name = "USA" },
        new { Id = 2, Name = "Canada" },
        new { Id = 3, Name = "UK" }
    }, "Id", "Name", 2);

    return View();
}

In the above code, we are creating a ViewBag property called “Countries” and assigning it a SelectList object. The SelectList object an of anonymous objects representing the dropdown options. Each object has two properties: “Id” and “Name.” We also the selected value as “2” in this example.

Now, let's create a view for the Index action method. Right-click on the “Index” action method and select “Add View.” Choose the “” template and click on “Add.”

Inside the view file, add the following HTML code to create a dropdown list:


@{
    var countries = ViewBag.Countries as SelectList;
}

@Html.DropDownList("Country", countries)

In the above code, we first retrieve the SelectList object from the ViewBag and assign it to a variable called “countries.” Then, we use the Html.DropDownList helper method to generate the dropdown list. The first parameter is the name of the dropdown list, and the second parameter is the SelectList object.

Now, if you run the application and navigate to the Index page, you will see a dropdown list with the “Canada” option selected by default. This is because we set the selected value to “2” in the HomeController.

the Selected Value

To change the selected value dynamically, you can modify the Index action method in the HomeController. For example, let's say you want to select “UK” as the default option. Update the code as follows:


public ActionResult Index()
{
    ViewBag.Countries = new SelectList(new[]
    {
        new { Id = 1, Name = "USA" },
        new { Id = 2, Name = "Canada" },
        new { Id = 3, Name = "UK" }
    }, "Id", "Name", 3);

    return View();
}

In the above code, we changed the selected value from “2” to “3” in the SelectList constructor. Now, if you run the application again, you will see “UK” selected by default in the dropdown list.

Conclusion

In this article, we learned how to create a dropdown list and set the selected value dynamically using ASP.NET MVC and HTML. We used the ViewBag to pass the SelectList object from the controller to the view and used the Html.DropDownList helper method to generate the dropdown list. By modifying the selected value in the SelectList constructor, we can change the default selected option.

Remember to include the necessary HTML tags, such as

for headings and

for paragraphs, to structure your content . Additionally, use the provided code snippets enclosed in

 tags to highlight the ASP.NET code examples.


Rate this post

Leave a Reply

Your email address will not be published. Required fields are marked *

Table of Contents