Tuesday, 22 February 2011

Asp.net mvc Dropdownlist population and selection

Populate the dropdownList.

Controller

     public ActionResult Create()
     {       
        var Degree=DegreeRepository.GetAllDegree();
        ViewData["Degree"] = new SelectList(Degree, "DegreeId", "DegreeName");
        return View();
     }

View

<%: Html.DropDownListFor(model => model.DegreeId, ViewData["Degree"]%>

Population and then selection of item in the dropdownList.

Controller

   public ActionResult Edit(int id)
   {       
     var User=UserRepository.GetUser(id);  // Get Single User
     var Degree=DegreeRepository.GetAllDegree();   // Get All Degrees
     ViewData["Degree"] = new selectList(Degree,"DegreeId",DegreeName",User.DegreeId);  
     return View();
   }

View
<%: Html.DropDownListFor(model => model.DegreeId, ViewData["Degree"]  %>

No comments:

Post a Comment