Search code examples
c#asp.net-mvcrepository-patternasp.net-mvc-viewmodel

Pass list value to view model class variable in ASP.NET MVC


I need to pass list of records to class variable (Users and Groups) in view model but I cannot seeing view model object (_UserAndGroupModel) recognizing its class object, I am not sure exactly what I am missing here.

ViewModel

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

each class user and group have their variables and collection Items

In following lines of code I need to pass list and and assign to user object and groups object within the view model

 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

Controller Method

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

        return PartialView("AddUserInGroup_Partial", _UserAndGroupList);
    }

View

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

@Html.DisplayNameFor(model => model) @Html.DisplayNameFor(model => model.LastName) @Html.DisplayNameFor(model => model.Age) @Html.DisplayNameFor(model => model.EmailAddress)

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

In view I cannot see ViewModel class variable when I do model => model.????


Solution

  • I would have done it a little different. You want to return a single view model to the view consisting of a list of users and a list of groups (please correct me if I am wrong). I would have initialised the lists to empty (containing no items) in the constructor of the view model:

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

    Then in your service layer you contruct the view model and populate the users and groups, and then return the view model to the calling method, in this case I would assume the controller's action method:

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

    Just make sure that User_Repository.GetAll() and Group_Repository.GetAll() have a return type of List<User> and List<Group>.

    And then your controller might look something like this:

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

    In your view you would have the following, accept the view model passed in by the action method and display the users and groups:

    @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>
    

    The code above has not been tested at all, there might be a couple of issues with the @ symbol in the view. I can see what you are trying to get at. A list of users in a table, then per line you want a dropdown list of user groups that you want to select and then send back to the controller. That is out of scope for this post, start a new one. I have helped you on the correct track, you just need to spend some more time reading up on ASP.NET MVC.

    I hope this helps :)