I am trying to get the selectlist of customers/products/categories from the database. the selectlist will only show the selectlist of customer/product/categories of the current user/current logged in user.
But I am only getting the first item belonging to the current logged user.
In the database, each users has applicationUserId
I am supposed to get all the list of customer/category/product of the current user in a selectlist
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();
}
Updated code
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();
}
after updating the code, I got
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);
? Is it task? Also, are you that the error occures in this code? Can you debug and make sure it ?