How To Declare ListBox Different Ways In MVC

In this article i explain you how to declare ListBox in mvc

    @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.ListBox("ListBox1",objSelectItem)
    }

2nd Way :-
    @using(Html.BeginForm())
    {
        List<SelectListItem> objSelectItem = new List<SelectListItem>();
        objSelectItem.Add(new SelectListItem() { Text = "Option1", Value = "1" });
        objSelectItem.Add(new SelectListItem() { Text = "Option2", Value = "2" });
        objSelectItem.Add(new SelectListItem() { Text = "Option3", Value = "3" });
        objSelectItem.Add(new SelectListItem() { Text = "Option4", Value = "4" });
        @Html.ListBox("ListBox", objSelectItem, new { id="ListBox1",size="10"})
    }
    3rd Way :-
    @using(Html.BeginForm())
    {
        List<SelectListItem> objSelectItem = new List<SelectListItem>();
        objSelectItem.Add(new SelectListItem() { Text = "Option1", Value = "1" });
        objSelectItem.Add(new SelectListItem() { Text = "Option2", Value = "2" });
        objSelectItem.Add(new SelectListItem() { Text = "Option3", Value = "3" });
        objSelectItem.Add(new SelectListItem() { Text = "Option4", Value = "4" });
        @Html.ListBox("ListBox", objSelectItem,new Dictionary<string,object>(){ {"id","ListBox1"},{"size","10"}})
    }

No comments:

Post a Comment