Click here to Skip to main content
15,881,173 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
how to bind dropdown in MVc 4 using Entity Framework..

and Also How dropDown is added in view i.e. in CSHTML file.
Posted

hi
XML
view

       @Html.DropDownListFor(model => model.RoleId, (IEnumerable<SelectListItem>)MvcApplicationIntranetApplication.Helpres.GenricHelpers.GetRolesList(), "--Select Roles --", new { @class = "required" })



helper class code:-
C#
public static SelectList GetRolesList()
        {
            try
            {
                using (TestDBEntities db = new TestDBEntities())
                {
                    List<Role> roleList = (from roles in db.Roles
                                           //where maritalStatus.Id == MaritalStatusId
                                           select roles).ToList<Role>();
                    //roleList.Insert(0, (new Roles { RoleId = 0, RoleName = "" }));

                    return new SelectList(roleList, "Id", "RoleName");
                }
            }
            catch (SqlException)
            {
                return null;
            }
            catch (EntityException)
            {
                return null;
            }
        }



PLease mark if it work for you
 
Share this answer
 
v2
C#
@Html.DropDownListFor(x => x.Group, (IEnumerable<SelectListItem>)ViewBag.SearchGroup)
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900