【发布时间】:2016-03-10 01:40:13
【问题描述】:
我有一个 ASP.NET MVC 应用程序。我使用个人用户帐户和身份 API 的方式是只有 canEdit 角色中的用户才能编辑数据(我的数据是产品)。 ASPNetUsers 和 Products 实体具有 1:N 关系(这只是一个示例)。因此,用户拥有(拥有)产品列表。
当用户请求http://localhost:1111/Products 时,页面应显示该用户之前登录的产品列表。
我不知道如何获取当前的 ApplicationUser 以及如何查询 products 表以获取属于某个用户的产品列表。
这是我的 ProductsController 的代码:
public class ProductsController : Controller
{
private ApplicationDbContext db = new ApplicationDbContext();
// GET: Products
[AllowAnonymous]
public ActionResult Index()
{
string currentUserId = User.Identity.GetUserId();
ApplicationUser currentUser = db.Users.FirstOrDefault(x => x.Id == currentUserId);
//How to modify these sentence to get the products of the current user??
Product product = db.Products.Find();
return View(db.Products.ToList());
}
....
【问题讨论】:
标签: c# asp.net asp.net-mvc