locked
How to Get all stores for current user to DropDownList .Net Core 3.1 Razor Page RRS feed

  • Question

  • User-642154842 posted

    Hi,

    I need to display all stores for the current user in a dropdown list, at the moment i  can display all the stores.

    <div class="col-9">
           @Html.DropDownListFor(m => m.UserStores, Model.UserStores,
                                                         "- Please select Account -",
                                                         new { @class = "form-control" }) </div>

    Model:

     [BindProperty]
            public IEnumerable<SelectListItem> UserStores { get; set; }
    
            public void OnGet()
            {
                var claimsIdentity = (ClaimsIdentity)User.Identity;
                var claim = claimsIdentity.FindFirst(ClaimTypes.NameIdentifier);
                UserStores = _unitOfWork.Stores.GetStoreListForDropDown();
    }

    Store Repository:

       public IEnumerable<SelectListItem> GetStoreListForDropDown()
            {
                return _db.Stores.Select(i => new SelectListItem()
                {
                    Text = i.Name,
                    Value = i.Id.ToString()
                });
            }

    IStore Repository:

     public interface IStoreRepository : IRepository<Stores>
        {
            IEnumerable<SelectListItem> GetStoreListForDropDown();        
        }

    So Farr all works OK, i need to select only stores for current user:

    When i add:

     UserStores = _unitOfWork.Stores.GetStoreListForDropDown(u => u.UserID == claim.Value);

    i get

    Error CS1501 No overload for method 'GetStoreListForDropDown' takes 1 arguments.

    thank you in advance.

    Tuesday, January 5, 2021 3:47 PM

Answers

  • User-642154842 posted

    I managed to get my users to the dropdownlist by modifying the repository, hope it will be useful for someone else:

     public IEnumerable<SelectListItem> GetApplicationUserListForDropDown(string userId)
          
                {
                    IEnumerable<SelectListItem> users = _db.ApplicationUser.OrderBy(u => u.UserName)
                        .Where(u => u.ID == userId)
                        .Select(u =>
                            new SelectListItem
                            {
                                Value = u.Id,
                                Text = u.UserName
                            }).ToList();
                    return new SelectList(users, "Value", "Text");
                }
                return null;

    is there room for improvement?

    Thanks

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, January 28, 2021 10:31 AM

All replies

  • User475983607 posted

    Your GetStoreListForDropDown() design does not accept a Func() parameter.  Filter the results using a Where...

     UserStores = _unitOfWork.Stores.GetStoreListForDropDown().Where(u => u.UserID == claim.Value);

    IMHO, a better approach is returning a filtered result rather than returning the entire table then filtering the results.

    Tuesday, January 5, 2021 3:57 PM
  • User-642154842 posted

    Hi mgebhard,

    thank you for the quick reply, can you give me an example with a filtered result please. 

    i was after the Func() parameter option.

    Thank you

    Tuesday, January 5, 2021 4:16 PM
  • User475983607 posted

    thank you for the quick reply, can you give me an example with a filtered result please. 

    i was after the Func() parameter option.

    That's up to you being the repository designer. 

    Tuesday, January 5, 2021 5:00 PM
  • User1312693872 posted

    Hi,bexasp

    I need to display all stores for the current user in a dropdown list

    This is the demo, mgebhard is right, you should use 'Where' to select the list:

    public class IndexModel : PageModel
        {
            private IStoreRepository _unitOfWork;
            public IndexModel(IStoreRepository storeRepository)
            {
    
                _unitOfWork = storeRepository;
            }
    
            [BindProperty]
            public IEnumerable<SelectListItem> UserStores { get; set; }
    
            public void OnGet()
            {
                UserStores = _unitOfWork.GetStoreListForDropDown().Where(u => u.Value == "1");
            }      
        }
    public interface IStoreRepository
        {
            IEnumerable<SelectListItem> GetStoreListForDropDown();
        }
    public class StoreRepository: IStoreRepository
        {
            private readonly MyDbContext _db;
            
            public StoreRepository(MyDbContext applicationDbContext)
            {
                _db = applicationDbContext;
            }
            public IEnumerable<SelectListItem> GetStoreListForDropDown()
            {
                return _db.Stores.Select(i => new SelectListItem()
                {
                    Text = i.Name,
                    Value = i.UserId.ToString()
                });
            }
        }

     UserStores = _unitOfWork.Stores.GetStoreListForDropDown(u => u.UserID == claim.Value);

    And if you want to use this, you should design a delegate in your Repository to get the parameter, that will be Inefficient, use 'where' to select

    will be better.

    Result:

    Best Regards,

    Jerry Cai

    Wednesday, January 6, 2021 3:30 AM
  • User-642154842 posted

    Hi Jerry,

    Thank you for the reply, your solution word for getting the stores for the users to the lish however it does not work for my goal.

    I need the value of the dropdownlist to be AccountNo "001" not userId. i need to use the dropdown value as a parameter to the Ajax call

    Select  Orders from ordertable where AccountNo=dropdoenlist.selectedvalue.

    Hope that makes sense.

    Many thanks

    Bex

    Friday, January 15, 2021 11:58 AM
  • User475983607 posted

    Thank you for the reply, your solution word for getting the stores for the users to the lish however it does not work for my goal.

    I need the value of the dropdownlist to be AccountNo "001" not userId. i need to use the dropdown value as a parameter to the Ajax call

    Select  Orders from ordertable where AccountNo=dropdoenlist.selectedvalue.

    Your repository design must support the requirement.    We cannot see the source code and have no way to provide any assistance in this regard.  Typically, we find new developer implement the repository pattern without understanding the pattern.  Later when needing to join tables the design no longer works or at least the code becomes complex.

    Anyway, you'll need to share the source code if you want a community code review and/or debugging support.

    Friday, January 15, 2021 12:17 PM
  • User-642154842 posted

    I managed to get my users to the dropdownlist by modifying the repository, hope it will be useful for someone else:

     public IEnumerable<SelectListItem> GetApplicationUserListForDropDown(string userId)
          
                {
                    IEnumerable<SelectListItem> users = _db.ApplicationUser.OrderBy(u => u.UserName)
                        .Where(u => u.ID == userId)
                        .Select(u =>
                            new SelectListItem
                            {
                                Value = u.Id,
                                Text = u.UserName
                            }).ToList();
                    return new SelectList(users, "Value", "Text");
                }
                return null;

    is there room for improvement?

    Thanks

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, January 28, 2021 10:31 AM