我正在使用 ASP Net Core,并使用 Razor Pages 创建了一个项目。
我有一个页面列出了与当前用户关联的产品,他可以添加新产品。
调用该方法后OnPost,我想刷新页面,使其显示用户的所有产品,而无需再次请求数据库
以下是我当前的解决方案:
public async Task OnGet()
{
var currentUser = await GetCurrentUser();
if (currentUser != null)
Products = await _dbContext.Products.Where(x => x.AtpUserId == currentUser.Id).ToListAsync();
}
public async Task OnPost(Product product)
{
var currentUser = await GetCurrentUser();
if (currentUser != null)
{
product.UserId = currentUser.Id;
product.Currency = "USD";
await _dbContext.Products.AddAsync(product);
await _dbContext.SaveChangesAsync();
Products = await _dbContext.Products.Where(x => x.UserId == currentUser.Id).ToListAsync(); // can I avoid this ?
}
}
Run Code Online (Sandbox Code Playgroud)
我不知道如何在没有从数据库重新加载数据的最后一行的情况下获得相同的结果。
Create …