Click here to Skip to main content
15,891,864 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
How do you bind the value in DropDownList using MVC?
Posted

Take a look on this
mvc dropdownlist binding[^]
 
Share this answer
 
1. We have created a list of SelectListItems inside the "BindingDropDown" method and passed it to the View. ssed it to the View.

C#
List<SelectListItem> items = new List<SelectListItem>();
items.Add(new SelectListItem
{
    Text = "item1",
    Value = "1"               
});
items.Add(new SelectListItem
{
    Text = "item2",
    Value = "2"                
});

ViewData["ListItems"] = items;//This will be used to bind to Dropdownlist


Binding Drop down at View

a. We can bind the dropdown list using html.Dropdownlist (it is a HTML helper class).

HTML
ListItems: <%= Html.DropDownList("ListItems")%>


Here we are passing "ListItems" that has been passed from the controller to the view.

b. Another overloaded method of the html.DropdownList class for binding a dropdown list.

HTML
<%= Html.DropDownList("SelectedItem", (IEnumerable<SelectListItem>)ViewData["ListItems"])%>


After run, u will get dropdown list with 2 items
 
Share this answer
 
The below links are best to understand the binding for html dropdown list in razor in MVC
Bind DropdownList[^]

Binding Dropdown C#[^]
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900