Ad

Search This Blog

Tuesday, June 30, 2015

How to bind the Dropdownlistfor from Data base in MVC4


In the Controller, we have to write the code in the below way :-

     List<OrganisationTypes> organisationTypes = new List<OrganisationTypes>();

     OrganisationRepository organisationRepository = new OrganisationRepository ();

      using (var dbcontext = new OrganisationContext())
      {
           organisationTypes = dbcontext.OrganisationTypes.ToList();

           var selectList = new SelectList(organisationTypes, "OrganisationId", "OrganisationName" );

           ViewBag.OrganisationTypes = selectList;
                 
       }


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


@Html.DropDownListFor(model => model.OrganisationId, 

(IEnumerable<SelectListItem>)ViewBag.OrganisationTypes, new { @class = "form-control" 

})

                 

No comments:

Post a Comment