Base solution for your next web application
Starts in:
01 DAYS
01 HRS
01 MIN
01 SEC
Open Closed

Get All User records in a list #1989


User avatar
0
sparkyjr created

Hi.

I'm trying to display names of all users of the current tenant in a dropdown. I tried using the following ways to get Users list, but didn't succeed

<ins>Method 1)</ins> In the controller, I tried injecting UserManagerand then in the action method, wrote the following code

var allUsers = _userManager.Users;
ViewBag.DdlUsers = new SelectList(allUsers, "Id", "value", null);

<ins>Method 2)</ins> In the controller, I tried injecting UserAppServiceand then in the action method, wrote the following code

var allUsers = var allUsers = await _userAppService.GetUsers(new GetUsersInput());
ViewBag.DdlUsers = new SelectList(allUsers, "Id", "value", null);

In both cases I'm getting the following error "The operation cannot be completed because the DbContext has been disposed."

I would also like to know if there is a way to get this list in Javascript as JSON


6 Answer(s)
  • User Avatar
    0
    ismcagdas created
    Support Team

    Hi,

    Actually both of your codes should work, I couldn't understand why do you get that error. Can you share full code of your controller if possible ?

    And you can get list of users from javascript using abp proxy services. You can check user list page in order to do that. It has the same logic but just adds paging parameters to query.

  • User Avatar
    0
    sparkyjr created

    Here is my controller. Please check the '_CreateOrEditKPI' Action

    using Abp.AutoMapper;
    using Abp.Domain.Repositories;
    using PlanQube.Authorization.Users;
    using PlanQube.Authorization.Users.Dto;
    using PlanQube.Entities;
    using PlanQube.Utilities;
    using PlanQube.Web.Areas.Mpa.Models.KPI;
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Threading.Tasks;
    using System.Web;
    using System.Web.Mvc;
    
    namespace PlanQube.Web.Areas.Mpa.Controllers
    {
        public class KPIController : Controller
        {
            private readonly IRepository<KPI> _KPIRepository;
            private readonly UserAppService _userAppService;
            private readonly UserManager _userManager;
    
            public KPIController(IRepository<KPI> KPIRepository, UserManager userManager, UserAppService userAppService)// UserAppService userAppService
            {
                _KPIRepository = KPIRepository;
                _userManager = userManager;
                _userAppService = userAppService;
            }
    
            // GET: Mpa/InitiativeKPI
            public async Task<ActionResult> Index()
            {
                var initiativeKPIs = (await _KPIRepository.GetAllListAsync()).MapTo<List<KPIViewModel>>().ToList();
                return View(initiativeKPIs);
            }
    
            public PartialViewResult _CreateOrEditKPI(int? id)
            {
                var viewModel = new KPIViewModel();
    
                if (id.HasValue)
                {
                    var output = _KPIRepository.Get((int)id);
                    viewModel = new KPIViewModel(output);
                }
    
                var allUsers = _userManager.Users;
                var allUsers = await _userAppService.GetUsers(new GetUsersInput());
                ViewBag.DdlUsers = new SelectList(allUsers , "Id", "value", null);
    
                return PartialView("_CreateOrEditKPIModal", viewModel);
            }
    
        }
    }
    

    For the second example, the code would be

    public async Task<PartialViewResult> _CreateOrEditKPI(int? id)
            {
                var viewModel = new KPIViewModel();
    
                if (id.HasValue)
                {
                    var output = _KPIRepository.Get((int)id);
                    viewModel = new KPIViewModel(output);
                }
    
                var allUsers = await _userAppService.GetUsers(new GetUsersInput());
                ViewBag.DdlUsers = new SelectList(allUsers , "Id", "value", null);
    
                return PartialView("_CreateOrEditKPIModal", viewModel);
            }
    
  • User Avatar
    0
    sparkyjr created

    In Javascript, I tried the following

    var _userService = abp.services.app.user;
    var allUsers = _userService.getUsers({ Filter : ""});
    

    and then in browser console, I tried to display allUser, it returns a deffered object as shown in the screenshot.....

    I also tried

    allUsers.done(function(result){
    console.log(result);
    });
    

    I would like to know how can I fetch the list in javascript

  • User Avatar
    0
    ismcagdas created
    Support Team

    Can you try this ?

    _userService.getUsers({ Filter : ""})
                .done(function(result) {
                    var users = result.items;
                    console.log(users);
                });
    
  • User Avatar
    0
    sparkyjr created

    Hi,

    _userService.getUsers({ Filter : ""})
                .done(function(result) {
                    var users = result.items;
                    console.log(users);
                });
    

    This particular code did worked for me. Thanks.

    I'm still trying to figure out why I'm not being able to fetch the list of users in my controller.

  • User Avatar
    0
    ismcagdas created
    Support Team

    Hi,

    The problem with your controller is, it's derived from regular MVC Controller. Can you try to derive it from [YourProjectNameControllerBase] ?