代码之家  ›  专栏  ›  技术社区  ›  Ramesh

再向默认index.cshtml razorpages asp.net core 2.1.0添加一个属性

  •  1
  • Ramesh  · 技术社区  · 6 年前

    我正在使用 Asp.Net Core 2.1.0 在一个项目中,我想额外添加一个 property 默认脚手架 Index.cshtml 页。

    这是我的实体,请建议。

    public class Role
    {
        public int RoleId { get; set; }
        public string RoleName { get; set; }
    
        public ICollection<UserRole> 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> 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; }
    }
    

    现在,默认的脚手架 索引文件 显示器 RoleID UserName 我想在哪里再加一个冒号,即 RoleName 哪一个在 Role entity .

    清单应该是 RoleID, RoleName, UserName

    这是我的脚手架页面模型。

    public class IndexModel : PageModel
    {
        private readonly Test.Models.TestContext _context;
    
        public IndexModel(Test.Models.TestContext context)
        {
            _context = context;
        }
    
        public IList<UserRole> UserRole { get;set; }
    
        public async Task OnGetAsync()
        {
            UserRole = await _context.UserRole
                .Include(u => u.Role)
                .Include(u => u.User).ToListAsync();
        }
    }
    

    请在不干扰其他页面的情况下帮助我,如 Edit, Detail, Delete.

    更新:index.cshtml中的代码

        @page
    @model Test.Pages.UserRoles.IndexModel
    
    @{
        ViewData["Title"] = "Index";
    }
    
    <h2>Index</h2>
    
    <p>
        <a asp-page="Create">Create New</a>
    </p>
    <table class="table">
        <thead>
            <tr>
                <th>
                    @Html.DisplayNameFor(model => model.UserRole[0].Role)
                </th>
                <th>
                    @Html.DisplayNameFor(model => model.UserRole[0].User)
                </th>
                <th></th>
            </tr>
        </thead>
        <tbody>
            @foreach (var item in Model.UserRole)
            {
                <tr>
                    <td>
                        @Html.DisplayFor(modelItem => item.Role.RoleId)
                    </td>
                    <td>
                        @Html.DisplayFor(modelItem => item.User.UserName)
                    </td>
                    <td>
                        <a asp-page="./Edit" asp-route-id="@item.Id">Edit</a> |
                        <a asp-page="./Details" asp-route-id="@item.Id">Details</a> |
                        <a asp-page="./Delete" asp-route-id="@item.Id">Delete</a>
                    </td>
                </tr>
            }
        </tbody>
    </table>
    
    1 回复  |  直到 6 年前
        1
  •  1
  •   D-Shih    6 年前

    @item.Role.RoleName index.chtml

    ViewModel view DB

    link