How to fill dropdown using view bag in MVC

 // GET: Home
        public ActionResult Index()
        {
            //Creating generic list
            List<SelectListItem> ObjList = new List<SelectListItem>()
            {
                new SelectListItem { Text = "Latur", Value = "1" },
                new SelectListItem { Text = "Pune", Value = "2" },
                new SelectListItem { Text = "Mumbai", Value = "3" },
                new SelectListItem { Text = "Delhi", Value = "4" },

            };
            //Assigning generic list to ViewBag
            ViewBag.Locations = ObjList;

            return View();
        }


 <div class="form-horizontal"> 
    <div class="col-md-12" style="margin-top:20px">  
       @Html.DropDownList("ObjList", 
(IEnumerable<SelectListItem>)ViewBag.Locations, new { id = "ddlLocations", @class = "form-control" })    
 </div> 
</div> 

Leave a Reply

Your email address will not be published. Required fields are marked *