使用viewmodel将数据绑定到asp.net-MVC中的html.dropdownlistFor

时间:2015-05-21 14:08:49

标签: c# asp.net-mvc repository-pattern html.dropdownlistfor model-view

我需要创建@html.dropdownlistFor,数据来自数据库。我有modelView,它结合了两个类。一个用户,另一个是Group。它假设在网格表中显示用户的记录以及可以为该特定用户选择组的下拉列表。

查看模型

公共类UserGroup_ViewModel     {

 public List<User> Users { get; set; }

 public List<Group> Groups { get; set; }

}

用户

 [Table("User")]
public class User
{
    public User() { }

    [Key]
    public int UserID { get; set; }

    [StringLength(250)]
    [Required]
    public string FirstName { get; set; }

    [StringLength(250)]
    [Required]
    public string LastName { get; set; }

    [Required]
    public int Age { get; set; }

    [StringLength(250)]
    [Required]
    public string EmailAddress { get; set; }

    public ICollection<UserInGroup> UserInGroup { get; set; }
}

 [Table("Group")]
public class Group
{
    public Group() { }

    [Key]
    public int GroupID { get; set; }

    [StringLength(250)]
    [Required]
    public string GroupName { get; set; }

    public ICollection<UserInGroup> UserInGroup { get; set; }
}

将数据分配给视图模型的类

public partial class UserManagement_Services 
{

    UserGroup_ViewModel _UserAndGroupModel = new UserGroup_ViewModel();

    public UserGroup_ViewModel GetUserAndGroups()
    {
        using(var _uow = new UserManagement_UnitOfWork())
        {
            _UserAndGroupModel.Users = _uow.User_Repository.GetAll().ToList();
            _UserAndGroupModel.Groups = _uow.Group_Repository.GetAll().ToList();

            return _UserAndGroupModel;
        }

    }
}

控制器方法

 public ActionResult AddUserInGroup()
    {
        var _UserAndGroupList = _userServices.GetUserAndGroups();

        return PartialView("AddUserInGroup_Partial", _UserAndGroupList);
    }

视图

 @model App.DAL.Model.UserGroup_ViewModel


<div>
  <table class="table">
    <tr>
        <th>
            @Html.DisplayName("First Name")
        </th>
        <th>
            @Html.DisplayName("LastName")
        </th>
        <th>
            @Html.DisplayName("Age")
        </th>
        <th>
            @Html.DisplayName("EmailAddress")
        </th>
        <th></th>
    </tr>

    @foreach (var item in Model.Users)
    {
        <tr>
            <td>
                @Html.DisplayFor(modelItem => @item.FirstName)
            </td>
            <td>
                @Html.DisplayFor(modelItem => item.LastName)
            </td>
            <td>
                @Html.DisplayFor(modelItem => item.Age)
            </td>
            <td>
                @Html.DisplayFor(modelItem => item.EmailAddress)
            </td>
            <td>

                @Html.DropDownList(?????????????

                <br/>



            </td>
        </tr>
    }

</table>

使用通用存储库模式访问数据,返回如下;

public IQueryable<TEntity> GetAll()
    {
        return _DbSet;
    }

我是否在正确的轨道上?实现或是另一种更好的方法来实现这一点(在单个视图中显示来自类和下拉列表的模型数据)...

1 个答案:

答案 0 :(得分:1)

@Html.DropDownList("Group", new SelectList(Model.Groups.Select(g => g.GroupName )))