Я использую ASP.NET Core 2.1.0 в проекте, где хочу добавить одно дополнительное свойство на страницу формирования шаблонов по умолчанию Index.cshtml.
Вот мой субъекты – предложите:
public class Role
{
public int RoleId { get; set; }
public string RoleName { get; set; }
public ICollection UserRole { get; set; }
}
public class User
{
[DatabaseGenerated(DatabaseGeneratedOption.None)]
public string UserName { get; set; }
public string Password { get; set; }
public string MobileNumber { get; set; }
public string Email { get; set; }
public ICollection UserRole { get; set; }
}
public class UserRole
{
public int Id { get; set; }
public string UserName { get; set; }
public int RoleId { get; set; }
[ForeignKey("RoleId")]
public Role Role { get; set; }
[ForeignKey("UserName")]
public User User { get; set; }
}
Теперь шаблон по умолчанию Index.cshtml отображает RoleID и UserName, где я хочу добавить еще один столбец, т.е. RoleName который доступен в объекте роли.
Список должен содержать RoleID, RoleName, UserName
Вот моя страница строительных лесов модель.
public class IndexModel : PageModel
{
private readonly Test.Models.TestContext _context;
public IndexModel(Test.Models.TestContext context)
{
_context = context;
}
public IList UserRole { get;set; }
public async Task OnGetAsync()
{
UserRole = await _context.UserRole
.Include(u => u.Role)
.Include(u => u.User).ToListAsync();
}
}
Пожалуйста, помогите мне, не нарушая другие страницы, такие как «Редактировать», «Сведения», «Удалить».
Обновление: разметка в Index.cshtml. :
@page
@model Test.Pages.UserRoles.IndexModel
@{
ViewData["Title"] = "Index";
}
Index
Create New
@Html.DisplayNameFor(model => model.UserRole[0].Role)
@Html.DisplayNameFor(model => model.UserRole[0].User)
@foreach (var item in Model.UserRole)
{
@Html.DisplayFor(modelItem => item.Role.RoleId)
@Html.DisplayFor(modelItem => item.User.UserName)
Edit |
Details |
Delete
}
Подробнее здесь: https://stackoverflow.com/questions/515 ... core-2-1-0