在asp.net-MVC中使用viewmodel与html.dropdownlistFor绑定数据

Use viewmodel to bind data with html.dropdownlistFor in asp.net-MVC

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

查看模型

public class 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; }
}

class 将数据分配给视图模型

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>

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

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

我走对了路吗??实现或者是另一种更好的实现方式(在单个视图中显示来自 class 的模型数据和来自另一个 class 的下拉列表)..

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