How To Declare DropDownList Different Ways In MVC

In this article i will explain you how to declare dropdownlist different ways
Step1:-
    @using (Html.BeginForm())
    {
        List<SelectListItem> objSelectItem = new List<SelectListItem>()
        {
            new SelectListItem{Text="Option1",Value="1"},
            new SelectListItem{Text="Option2",Value="2"},
            new SelectListItem{Text="Option3",Value="3"},
            new SelectListItem{Text="Option4",Value="4"}
        };
        @Html.DropDownList("DropDownList1",objSelectItem)
    }
2nd Way :-
  @Html.DropDownList("DropDownList1", new List<SelectListItem>()
{
    new SelectListItem{Text="Option1",Value="1"},
    new SelectListItem{Text="Option2",Value="2"},
    new SelectListItem{Text="Option3",Value="3"},
    new SelectListItem{Text="Option4",Value="4"}

})

3rd Way :-
    @Html.DropDownList("DropDownList1",new SelectListItem[]{
    new SelectListItem{Text="Option1",Value="1"},
    new SelectListItem{Text="Option2",Value="2"},
    new SelectListItem{Text="Option3",Value="3"},
    new SelectListItem{Text="Option4",Value="4"}
      })
4th Way:-
    @Html.DropDownList("DropDownList1", new List<SelectListItem>()
{
    new SelectListItem{Text="Option1",Value="1"},
    new SelectListItem{Text="Option2",Value="2"},
    new SelectListItem{Text="Option3",Value="3"},
    new SelectListItem{Text="Option4",Value="4"}
},"Select Option")
5th Way :-
    @Html.DropDownList("DropDownList1", new List<SelectListItem>()
{
    new SelectListItem{Text="Option1",Value="1"},
    new SelectListItem{Text="Option2",Value="2"},
    new SelectListItem{Text="Option3",Value="3"},
    new SelectListItem{Text="Option4",Value="4"}
},new Dictionary<string,object>(){{"Id","ddl1"}})
6th Way :-
    @Html.DropDownList("DropDownList1", new List<SelectListItem>()
{
    new SelectListItem{Text="Option1",Value="1"},
    new SelectListItem{Text="Option2",Value="2"},
    new SelectListItem{Text="Option3",Value="3"},
    new SelectListItem{Text="Option4",Value="4"}
}, "Select Option",new Dictionary<string, object>() { { "Id", "ddl1" } })
7th Way :-
@Html.DropDownList("DropDownList1", new List<SelectListItem>()
{
    new SelectListItem{Text="Otion1",Value="1"},
    new SelectListItem{Text="Option2",Value="2"},
    new SelectListItem{Text="Option3",Value="3"},
    new SelectListItem{Text="Option4",Value="4"}
}, "Select Option", new { id="ddl1"})

No comments:

Post a Comment