Tuesday, 22 February 2011

Asp.net mvc having multiselect with the listbox

Populate the Listbox with MultiSelect.
Controller:
  public ActionResult Create()
  { 
     var Degree=DegreeRepository.GetAllDegree();      
     ViewData["Degree"] = new MultiSelectList(Degree, "DegreeId", "DegreeName");
     return View(); 
  }

Here I want to mention if you want the single selection for the Listbox you can use SelectList as I mentioned here

View
<%: Html.ListBoxFor(model => model.DegreeId, ViewData["Degree"] as MultiSelectList) %>  

Population and then selection of item in the dropdownList. 

Controller:

  public ActionResult Edit(int id)
  {       
     var Users=UserRepository.GetUser(id);    
     var Degree=DegreeRepository.GetAllDegree();   
     ViewData["Degree"] = new SectList(Degree,"DegreeId","DegreeName",Users.DegreeId);
     return View();
  }


View:

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

No comments:

Post a Comment