2 Replies - 183 Views - Last Post: 02 April 2019 - 02:07 PM Rate Topic: -----

#1 Exceedinglife   User is offline

  • D.I.C Head

Reputation: 5
  • View blog
  • Posts: 219
  • Joined: 01-July 12

Question with asp.net making list of object from model for view

Posted 01 April 2019 - 09:51 PM

I have a ASP.Net C# site and I have a controller that I am trying to get a list of my users with role admin and a list of my users with role user. I have a model that has a list of each. I would like to get a list of each of them so I can use it in my view. Here is what I have and some of what I tried.
//models Iím using                                                                      
  public class AdminModel
        {
            public string UserId { get; set; }
            public string Name { get; set; }
        }
        public class UserModel
        {
            public string UserId { get; set; }
            public string Name { get; set; }
            public bool SelectedUsers { get; set; }
            public string AssignToAdmin { get; set; }
        }
        public class AssignRolesModel
        {
            public List<AdminModel> lstAdmins { get; set; }
            [Required(ErrorMessage = "Choose Admin")]
            public string UserId { get; set; }
            public List<UserModel> lstUsers { get; set; }
            public int? AssignToAdmin { get; set; }
            public int? CreatedBy { get; set; }
        }                                                                                     // Controller                                                                              public class SuperAdminController : Controller
        {
        private ApplicationDbContext context;

        public SuperAdminController()
        {
            context = new ApplicationDbContext();
        }                                                                                 // ASSIGN ADMINISTRATOR
        public ActionResult AssignAdmin()
        {
            var lstAdmins = from users in context.Users
                            from roles in users.Roles
                            join r in context.Roles 
                            on roles.RoleId equals r.Id
                            where r.Name.Equals("Admin")
                            select new AssignRolesModel()
                            {
                                lstAdmins = (users.Id, users.Name),

                            }


            return View();
        } //my view                                                                   @model MVC_TimeSh.Models.AssignRolesModel
<div class="row">
                <div class="col-md-4">
                    @Html.DropDownListFor(a => a.UserId,
                     new SelectList(Model.lstAdmins, "UserId", "Name"),
                     new { @class = "form-control" })
                    @Html.ValidationMessageFor(a => a.UserId)
                </div>
                <div class="col-md-4"></div>
                <div class="col-md-4"></div>
            </div>

Not sure how to make a list of both of just one in my model.

Is This A Good Question/Topic? 0
  • +

Replies To: Question with asp.net making list of object from model for view

#2 modi123_1   User is online

  • Suitor #2
  • member icon



Reputation: 15160
  • View blog
  • Posts: 60,694
  • Joined: 12-June 08

Re: Question with asp.net making list of object from model for view

Posted 02 April 2019 - 05:37 AM

Why not have a boolean property in your 'UserModel' called 'isAdmin'?

Not sure what your db table looks like, but that's typically a column I have.. or a small integer for 'Role Level'.. 0 = no access, 1= user, 2 = admin, etc.
Was This Post Helpful? 0
  • +
  • -

#3 Exceedinglife   User is offline

  • D.I.C Head

Reputation: 5
  • View blog
  • Posts: 219
  • Joined: 01-July 12

Re: Question with asp.net making list of object from model for view

Posted 02 April 2019 - 02:07 PM

I solved it with creating a new method to populate my object

        public List<AdminModel> AdminList()
        {
            var lstAdmin = (from u in context.Users
                            from r in u.Roles
                            join rr in context.Roles
                            on r.RoleId equals rr.Id
                            where rr.Name.Equals("Admin")
                            select new AdminModel()
                            {
                               UserId = u.Id,
                               Name = u.Name

                            }).ToList();

            List<AdminModel> listAdmins = lstAdmin;
            //lstAdmins.Add(new AdminModel)
            return listAdmins;
        }

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1