Help with MVC Dropdownlist generation [Resolved]

Posted by Rasagna under ASP.NET MVC on 3/14/2016 | Points: 10 | Views : 1275 | Status : [Member] | Replies : 1
Hii,
I have the following code.Can anyone help me with the view code to populate IEnumerable<SelectListItem>GroupsList data into the dropdownlist..where GroupsList is a list of records in a table with id and name as columns.I also need a default ''select value" in dropdown list .


My controller code:
  public ActionResult GetMyGroups()
{
var groupslist = _adminSystemServices.GetGroups();
var model = new GroupsViewModel
{
GroupsList = groupslist.Select(x => new SelectListItem
{
Text = x.Name,
Value = x.Id.ToString()
})
};
return View(model);
}


Model:
 public class GroupsViewModel : HeaderViewModel
{
public Guid Id { get; set; }
public String Name { get; set; }
public Guid GroupId { get; set; }
public Guid ClaimId { get; set; }
public String Description { get; set; }
public List<ClaimViewModel> AvailableClaimsList { get; set; }
public IEnumerable<SelectListItem> GroupsList { get; set; }
public List<ClaimViewModel> GroupClaimsList { get; set; }
public ClaimViewModel ClaimViewModel { get; set; }
}





Responses

Posted by: Allemahesh on: 3/28/2016 [Member] [MVP] Silver | Points: 50

Up
0
Down

Resolved
Try the below code:-

@Html.DropDownListFor(M => M.Id, GroupsList)


Rasagna, if this helps please login to Mark As Answer. | Alert Moderator

Login to post response