传递列表值以在ASP.NET MVC中查看模型类变量

时间:2015-05-21 09:52:04

标签: c# asp.net-mvc repository-pattern asp.net-mvc-viewmodel

我需要将视图列表传递给视图模型中的类变量(用户和组),但我无法看到视图模型对象(_UserAndGroupModel)识别其类对象,我不确定这里到底是什么。

视图模型

 public class UserGroup_ViewModel
{
    public User Users { get; set; }
    public Group Groups { get; set; }
}

每个类用户和组都有自己的变量和集合项

在以下代码行中,我需要传递列表,并在视图模型中分配给用户对象和组对象

 public partial class UserManagement_Services 
{
    List<UserGroup_ViewModel> _UserAndGroupModel = new List<UserGroup_ViewModel>();

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

         //error here   ??????????????
        }
    }
}

enter image description here

控制器方法

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

        return PartialView("AddUserInGroup_Partial", _UserAndGroupList);
    }

视图

@model IEnumerable<App.DAL.Model.UserGroup_ViewModel>

                                           @ Html.DisplayNameFor(model =&gt; model)                                           @ Html.DisplayNameFor(model =&gt; model.LastName)                                           @ Html.DisplayNameFor(model =&gt; model.Age)                                           @ Html.DisplayNameFor(model =&gt; model.EmailAddress)                                   

    @foreach (var item in Model)
    {....

在视图中,当我执行model =&gt;时,我看不到ViewModel类变量模型。????

3 个答案:

答案 0 :(得分:3)

我会做得有点不同。您希望将单个视图模型返回到包含用户列表和组列表的视图(如果我错了,请更正我)。我会在视图模型的构造函数中初始化列表以清空(不包含任何项目):

public class UserGroup_ViewlModel
{
     public UserGroup_ViewlModel()
     {
          Users = new List<User>();
          Groups = new List<Group>();
     }

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

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

然后在您的服务层中构建视图模型并填充用户和组,然后将视图模型返回到调用方法,在这种情况下,我将假设控制器的操作方法:

public class UserManagement_Services
{
     public UserGroup_ViewModel GetUserAndGroups()
     {
          UserGroup_ViewModel _UserAndGroupModel = new UserGroup_ViewModel();

          using(var _uow = new UserManagement_UnitOfWork())
          {
              _UserAndGroupModel.Users = _uow.User_Repository.GetAll();
              _UserAndGroupModel.Groups = _uow.Group_Repository.GetAll();
          }

          return _UserAndGroupModel;
     }
}

请确保User_Repository.GetAll()Group_Repository.GetAll()的返回类型为List<User>List<Group>

然后你的控制器看起来像这样:

public class UserController : Controller
{
     private IUserManagement_Services userManagementService;

     public UserController(IUserManagement_Services userManagementService)
     {
          this.userManagementService = userManagementService;
     }

     public ActionResult Index()
     {
          // The service will return a view model already populated with the users and groups
          UserGroup_ViewlModel model = userManagementService.GetUserAndGroups();

          return View(model);
     }
}

在您的视图中,您将拥有以下内容,接受操作方法传入的视图模型并显示用户和组:

@model MyProject.Models.UserGroup_ViewlModel

 <table>
      <thead>
           <tr>
                <th>First Name</th>
                <th>Last Name</th>
                <th>Groups</th>
           </tr>
      </thead>
      <tbody>
           @foreach (var user in Model.Users)
           {
                <tr>
                     <td>@user.FirstName</td>
                     <td>@user.LastName</td>
                     <td>
                          <select>
                               @foreach (var group in Model.Groups)
                               {
                                    <option>group.GroupName</option>
                               }
                          </select>
                     </td>
                </tr>
           }
      </tbody>
 </table>

上面的代码根本没有经过测试,视图中的@符号可能存在​​一些问题。我可以看到你想要得到什么。表中的用户列表,然后每行您需要一个要选择的用户组的下拉列表,然后发送回控制器。这个帖子超出了这个帖子的范围,开始一个新的。我已经帮助你找到正确的轨道,你只需要花更多的时间阅读ASP.NET MVC。

我希望这会有所帮助:)

答案 1 :(得分:2)

您的错误来自此行

List<UserGroup_ViewModel> _UserAndGroupModel = new List<UserGroup_ViewModel>();

_UserAndGroupModelList,因此当您使用

进行设置时

_UserAndGroupModel.Users = ..

这是无效的因为Users是列表中的属性,而不是列表本身。

但是,这个

_UserAndGroupModel.First().Users = ...

会起作用。

答案 2 :(得分:0)

这里是答案,非常感谢Ammar的帮助!

public class UserGroup_ViewModel
{
    public List<User> Users { get; set; }
    public List<Group> Groups { get; set; }

}

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

    }