在视图中显示模型属性

时间:2018-06-29 19:26:16

标签: c# razor asp.net-core

我想知道如何在视图中显示model.property而不用在视图中将模型变量设为IEnumerable类型;而且,我应该在控制器中使用什么而不是SingleOrDefault来显示特定属性的每一行?

代码如下:

模型-Roles.cs

    [Key]
    public string Id { get; set; }
    public string Name { get; set; }
    public string NormalizedName { get; set; }
    public string ConcurrencyStamp { get; set; }
    public int FunctionId { get; set; }

    [ForeignKey("FunctionId")]
    public Functions Functions { get; set; }

Model -Function.cs

[Key]
    public int FunctionId { get; set; }
    public string FunctionName { get; set; }
    public bool CanEdit { get; set; }
    public bool CanAdd { get; set; }
    public bool CanDel { get; set; }
    public bool CanView { get; set; }

控制器-RolesController.cs

public async Task<IActionResult> Edit(string id)
    {
        if (id == null)
        {
            return NotFound();
        }

        var aspNetRoles = await _context.AspNetRoles.SingleOrDefaultAsync(m => m.Id == id);
        if (aspNetRoles == null)
        {
            return NotFound();
        }
        ViewData["FunctionId"] = new SelectList(_context.Functions, "FunctionId", "FunctionId", aspNetRoles.FunctionId);
        return View(aspNetRoles);
    }

然后我想获得的视图 角色/Edit.cshtml

<div class="flex-row col-8">
        @foreach(var item in Model //what to do?)
             {
                @Html.DisplayFor(m => m.Functions.FunctionName) 
                <input type="checkbox" asp-for="Functions.CanAdd"  />  
                <input type="checkbox" asp-for="Functions.CanEdit" /> 
                <input type="checkbox" asp-for="Functions.CanDel"  />  
                <input type="checkbox" asp-for="Functions.CanView" />    
             }

谢谢!

0 个答案:

没有答案