0

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.????

1
  • Don't use var, if you know that _userServices.GetUserAndGroups() returns a UserGroup_ViewModel then use it. Commented May 21, 2015 at 10:57

3 Answers 3

3

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 :)

Sign up to request clarification or add additional context in comments.

11 Comments

That is correct, in addition my GetAll() method is returning IQueryable and I am getting another issue to populate model in view from viewmodel
What is your issue? Explain what you are trying to do.
Give me min I upload screen shot
I cannot access to Users and Groups from viewmodel in view. My view is strongly typed with view
I updated controller and view code in my question above
|
1

Your error is stems from this line

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

_UserAndGroupModel is a List, so when you set with

_UserAndGroupModel.Users = ...

this is invalid becuase Users is a property of an item in the list, not the list itself.

However, this

_UserAndGroupModel.First().Users = ...

would work for example.

3 Comments

then how I assign list in GetUserAndGroups() method??
@toxic, by changing your ViewModel like my suggestion above. It should then stop giving you an error. Let me know if it still does
No I am still getting error, I have upload screen shot of error in my question
0

here is answer and many thanks to Ammar for help!

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

    }

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.