【问题标题】:Use viewmodel to bind data with html.dropdownlistFor in asp.net-MVC在asp.net-MVC中使用viewmodel与html.dropdownlistFor绑定数据
【发布时间】:2015-05-21 14:08:49
【问题描述】:

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

查看模型

公共类 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;
    }

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

【问题讨论】:

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


    【解决方案1】:
    @Html.DropDownList("Group", new SelectList(Model.Groups.Select(g => g.GroupName )))
    

    【讨论】:

      猜你喜欢
      • 2011-01-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-12-16
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多