我正在处理一个小项目并尝试显示全名(我在从 IdentityUser 继承的 ApplicationUser 模型中添加的)和模型下方:
public class ApplicationUser : IdentityUser
{
public int PersonnalNumber { get; set; }
public string FullName { get; set; }
}
这是 SystemDetails 模型与应用程序用户的关系:
public class SystemDetails
{
[Key]
public int Id { get; set; }
[Required]
[Display(Name = "System Name")]
public string SystemName { get; set; }
[Required]
[Display(Name = "Admin Name")]
public string SystemAdminId { get; set; }
public string ServerNames { get; set; }
[ForeignKey("SystemAdminId")]
public virtual ApplicationUser ApplicationUser { get; set; }
}
这是索引视图:
@if (Model.Count() > 0)
{
<table class=" table table-striped border">
<tr class="table-secondary">
<th>
@Html.DisplayNameFor(m => m.SystemName)
</th>
<th>
@Html.DisplayNameFor(m => m.SystemAdminId)
</th>
<th></th>
<th></th>
</tr>
@foreach (var item in Model)
{
<tr>
<td>
@Html.DisplayFor(m => item.SystemName)
</td>
<td>
@Html.DisplayFor(m => item.ApplicationUser.FullName)
</td>
<td>
<partial name="_TableButtonPartial" model="@item.Id" />
</td>
</tr>
}
一切正常,除了 FullName 未显示在索引页面上,我可以显示 SystemAdminId,这是注册用户 ID,但是当我尝试使用 Applicationuser.FullName 显示 FullName 时,它没有显示任何内容!当我在 SystemDetails Controller 上创建记录时,它被正确保存在数据库中,如下所示:
这是创建操作:
[HttpPost,ActionName("Create")]
[ValidateAntiForgeryToken]
public async Task<IActionResult> CreatePost()
{
List<ApplicationUser> userList = new List<ApplicationUser>();
if (!ModelState.IsValid)
{
return NotFound();
}
userList = await (from user in _db.ApplicationUser select user).ToListAsync();
ViewBag.usersList = userList;
_db.SystemDetails.Add(SystemDetails);
await _db.SaveChangesAsync();
return RedirectToAction(nameof(Index));
}
感谢任何帮助
在控制器中,当查询SystemDetails
列表时,您应该加载相关的ApplicationUser
:
using Microsoft.EntityFrameworkCore;
var result = _dbContext.SystemDetails.Include(f => f.ApplicationUser).ToList();
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句