Ad

Search This Blog

Tuesday, June 30, 2015

How to bind the DropdownList From Data base in MVC4

Code in C#

           List<Category> categoryTypes = new List<Category>(); ;

            CategoryRepository categoryRepository = new CategoryRepository ();

            using (var dbcontext = new CategoryContext())
            {
                categoryTypes = dbcontext.Category.ToList();

                var selectList = new SelectList(categoryTypes, "CategoryID  ", "Category");

                ViewBag.categoryTypes = selectList;

            }


In DBContext Class we have to do in the following manner :-

public DbSet<Category> Category{ get; set; }


In View , we have to mention  in below way :-

  @Html.DropDownList("Category", ViewBag.categoryTypes as SelectList)

No comments:

Post a Comment