【问题标题】:How to get SelectList for the current user如何获取当前用户的 SelectList
【发布时间】:2020-02-06 23:07:04
【问题描述】:

我正在尝试从数据库中获取客户/产品/类别的选择列表。选择列表将仅显示当前用户/当前登录用户的客户/产品/类别的选择列表。

但我只得到属于当前登录用户的第一项。

在数据库中,每个用户都有applicationUserId

我应该在选择列表中获取当前用户的所有客户/类别/产品列表

 public IActionResult Create()
        {
            var currentUser = _userManager.GetUserAsync(HttpContext.User);

            ViewData["CustomerId"] = new SelectList(_context.Customers.Where(c => c.ID == currentUser.Id), "ID", "Name") ;


            ViewData["ProductId"] = new SelectList(_context.Products.Where(p => p.ProductId == currentUser.Id), "ProductId", "ProductName");


            ViewData["CategoryId"] = new SelectList(_context.Categories.Where(p =>p.CategoryId == currentUser.Id) , "CategoryId", "CategoryName");

            return View();
        }

更新代码

 public IActionResult Create()
        {
            var currentUser = _userManager.GetUserAsync(HttpContext.User);

            string userId = currentUser.Id.ToString();
            ViewData["CustomerId"] = new SelectList(_context.Customers.Where(c => c.ApplicationUserId == userId), "ID", "Name");

            ViewData["ProductId"] = new SelectList(_context.Categories.Where(p => p.ApplicationUserId == userId), "ProductId", "ProductName");

            ViewData["ProductId"] = new SelectList(_context.Products.Where(p => p.ApplicationUserId == userId), "ProductId", "ProductName");

            return View();
        }

更新代码后,我得到了

InvalidOperationException: A second operation started on this context before a previous operation completed. This is usually caused by different threads using the same instance of DbContext, however instance members are not guaranteed to be thread safe. This could also be caused by a nested query being evaluated on the client, if this is the case rewrite the query avoiding nested invocations.

【问题讨论】:

  • _userManager.GetUserAsync(HttpContext.User); 的返回类型是什么?是任务吗?另外,您是否认为此代码中出现错误?你能调试并确保它吗?

标签: c# asp.net-mvc entity-framework asp.net-core entity-framework-core


【解决方案1】:

您在查询中使用的外键错误。

ViewData["ProductId"] = new SelectList(_context.Products.Where(p => p.ProductId == currentUser.Id), "ProductId", "ProductName");

应该是这样的,而不是这样,

ViewData["ProductId"] = new SelectList(_context.Products.Where(p => p.UserId == currentUser.Id), "ProductId", "ProductName");

字段名称取决于您的表结构。

【讨论】:

  • 当我这样做时ViewData["ProductId"] = new SelectList(_context.Products.Where(p => p.applicationUserId == currentUser.Id), "ProductId", "ProductName"); 我得到了错误operator "==" cannot be applied to operand 'string' and 'int'
  • 我相信你的 applicationUserId 定义为字符串呢?所以你可以做类似string userId = currentUser.Id.ToString(); ViewData["ProductId"] = new SelectList(_context.Products.Where(p => p.applicationUserId == userId), "ProductId", "ProductName");
  • 现在我正在匹配商店 ID 和当前用户 ID。它适用于一个,但是当我添加另一个选择列表代码时,我得到了`InvalidOperationException:在前一个操作完成之前在此上下文上启动了第二个操作。这通常是由使用相同 DbContext 实例的不同线程引起的,但是不能保证实例成员是线程安全的。这也可能是由在客户端上评估嵌套查询引起的,如果是这种情况,请重写查询以避免嵌套调用。'
  • 你能用代码更新问题吗?跟进会更容易
【解决方案2】:

问题是您没有等待第一个查询,因此在第一个查询完成之前发出后续查询。也就是说,你需要添加await关键字。

var currentUser = await _userManager.GetUserAsync(HttpContext.User);

但是,由于您在这里只需要用户的 id,因此没有必要为此访问数据库。做吧:

var userId = User.FindFirstValue(ClaimTypes.NameIdentifier);

【讨论】:

  • 谢谢,它现在工作正常。我添加了更新返回到异步任务public async Task<IActionResult> Create()
【解决方案3】:

谢谢, @serdar 和 @chris-pratt 的两个建议帮助我修复了我的代码。

后来我出来了

  public async Task<IActionResult> Create()
        {
            var currentUser = await _userManager.GetUserAsync(HttpContext.User);

            string userId = currentUser.Id.ToString();
            ViewData["CustomerId"] = new SelectList(_context.Customers.Where(c => c.ApplicationUserId == userId), "ID", "Name");

            ViewData["CategoryId"] = new SelectList(_context.Categories.Where(p => p.ApplicationUserId == userId), "CategoryId", "CategoryName");

            ViewData["ProductId"] = new SelectList(_context.Products.Where(p => p.ApplicationUserId == userId), "ProductId", "ProductName");



            return View();
        }

【讨论】:

    猜你喜欢
    • 2014-08-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-05-18
    • 2013-02-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多