开发者

DropDownList View Model

I'm trying to produce a dropdownlist for GetAllRoles using the role provider.

I can produce the drop down in a controller using ViewData but I would like to use a View Model to produce the dropdown, but I'm unsure of the best way to create the list using a View Model?

public ActionResult GetAllRoles()
{
   ViewData["Roles"] = new SelectList(Roles.GetAllRoles());
   ret开发者_如何转开发urn View(); 
}


Depending on how you want to do it,

var allroles = new SelectList(Roles.GetAllRoles()); 
return View(allRoles);

Create a strongly typed view of type SelectList.

Based on your recent comment.

public SelectList GetAllRoles() { 
  var AllRoles = new SelectList(Roles.GetAllRoles()); 
  return AllRoles; 
} 

public class RoleViewModel {
  [Required]
  [DisplayName("AllRoles")]
  public SelectList AllRoles { get; set; }
}


public ActionResult GetAllRoles() 
{ 
   var roleViewModel = new RoleViewModel {
      AllRoles = GetAllRoles();
   };
   return View(roleViewModel);  
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜