Shalvin Interests

Saturday, February 8, 2014

Asp .Net MVC @Html.DropDownList

Hard coding the Drop Down List
Countries
@Html.DropDownList("Countries",
     new  List<SelectListItem> {
     new SelectListItem {Text = "India" },
     new SelectListItem {Text = "US" }
})

Fillling DropDownList with Database data

public ActionResult Index()
{
    ContactGroups cg = new ContactGroups();
    var cgs = cg.GetGroups();
    ViewBag.groups =new SelectList(cgs,"GroupId","GroupName");

    return View();
}
<div>
    @Html.DropDownList("groups", "select Group") 

</div>
Here groups is the name of ViewBag.

No comments:

Post a Comment