如何在 Razor 页面中获取用户模型字段?
How to get User model field inside Razor page?
我正在使用带有身份的 asp.net 核心。对于用户,我有这个 class:
public class User : IdentityUser
{
public List<Rate> Rates { get; set; }
}
我想在 Razor 中获取 Rates,如何使用 Name 字段完成此操作 (User.Identity.Name)。
First, please confirm whether you have successfully added a one to many
relation of Rate table
for Identityuser and generated the Rate
table in the database.
请将您的项目中的所有 Identityuser 引用(用户 class 继承的除外)更改为您的自定义用户 class,包括相关的查看页面和启动class.
然后按如下方式更改 ApplicationDbContext:
public class ApplicationDbContext : IdentityDbContext<User>
{
public DbSet<Rate> Rate { get; set; }
public ApplicationDbContext(DbContextOptions<ApplicationDbContext> options)
: base(options)
{
}
}
然后执行迁移命令。更详细的,参考这个video.
这是我的评分 Class:
public class Rate
{
[Key]
public int Id { get; set; }
public int rate { get; set; }
public virtual User User { get; set; }
}
完成以上操作后,add relevant Rates data
到数据库
然后启动项目,登录用户信息,然后在对应的action和view中使用如下代码显示关联的Name字段的Rates:
[Authorize]
public class AccountController : Controller
{
private readonly ApplicationDbContext _context;
public AccountController(ApplicationDbContext context)
{
_context = context;
}
public async Task<IActionResult> Index()
{
var rates = await _context.Rate.Include(host => host.User).Where(x => x.User.UserName == User.Identity.Name).ToListAsync();
return View(rates);
}
}
Razor 视图:
@model IEnumerable<Rate>
@{
ViewData["Title"] = "Index";
Layout = "~/Views/Shared/_Layout.cshtml";
}
<h1>Index</h1>
UserName: @User.Identity.Name related Rates data:
<table class="table table-bordered">
<tr>
<th>Id</th>
<th>Rate</th>
</tr>
@foreach (var item in Model)
{
<tr>
<td>@item.Id</td>
<td>@item.rate</td>
</tr>
}
</table>
测试结果如下:
我正在使用带有身份的 asp.net 核心。对于用户,我有这个 class:
public class User : IdentityUser
{
public List<Rate> Rates { get; set; }
}
我想在 Razor 中获取 Rates,如何使用 Name 字段完成此操作 (User.Identity.Name)。
First, please confirm whether you have successfully added a
one to many
relation ofRate table
for Identityuser and generated the Rate table in the database.
请将您的项目中的所有 Identityuser 引用(用户 class 继承的除外)更改为您的自定义用户 class,包括相关的查看页面和启动class.
然后按如下方式更改 ApplicationDbContext:
public class ApplicationDbContext : IdentityDbContext<User>
{
public DbSet<Rate> Rate { get; set; }
public ApplicationDbContext(DbContextOptions<ApplicationDbContext> options)
: base(options)
{
}
}
然后执行迁移命令。更详细的,参考这个video.
这是我的评分 Class:
public class Rate
{
[Key]
public int Id { get; set; }
public int rate { get; set; }
public virtual User User { get; set; }
}
完成以上操作后,add relevant Rates data
到数据库
然后启动项目,登录用户信息,然后在对应的action和view中使用如下代码显示关联的Name字段的Rates:
[Authorize]
public class AccountController : Controller
{
private readonly ApplicationDbContext _context;
public AccountController(ApplicationDbContext context)
{
_context = context;
}
public async Task<IActionResult> Index()
{
var rates = await _context.Rate.Include(host => host.User).Where(x => x.User.UserName == User.Identity.Name).ToListAsync();
return View(rates);
}
}
Razor 视图:
@model IEnumerable<Rate>
@{
ViewData["Title"] = "Index";
Layout = "~/Views/Shared/_Layout.cshtml";
}
<h1>Index</h1>
UserName: @User.Identity.Name related Rates data:
<table class="table table-bordered">
<tr>
<th>Id</th>
<th>Rate</th>
</tr>
@foreach (var item in Model)
{
<tr>
<td>@item.Id</td>
<td>@item.rate</td>
</tr>
}
</table>
测试结果如下: