Sample Code:Bind the value to Dropdownlist without using Model

Monday, 3 February 2014

Controller:


   public ActionResult Binddropdown()
        {
            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;
            return View();
        }

View:

@{
    ViewBag.Title = "Binddropdown";
    Layout = "~/Views/Shared/_Layout.cshtml";
}

<style type="text/css"> 
        .DropDownListCssClass 
        { 
            color:Snow; 
            background-color:DarkOrchid; 
            font-family:Comic Sans MS; 
            font-size:large; 
            font-style:italic; 
            } 
    </style>

<h2>Binddropdown Without using Models</h2>

<br />
<br />

@Html.DropDownList("SelectedItem",(IEnumerable<SelectListItem>)ViewData["ListItems"],new{ @class="DropDownListCssClass"})

Note:

  Refer the css class by using @class attribute in the helper class.



ScreenShot:





Conclusion:

    Bind the value to dropdownlist by using genericlist.