单击该项目后显示数据库中的特定数据

时间:2019-03-11 16:16:31

标签: c# asp.net-mvc-4 model-view-controller roles

如何列出已分配给角色的所有用户。这是我的模特。

namespace Comtrex_ICU.Models
{
  public class UsersContext : DbContext
  {
    public UsersContext()
      : base("DefaultConnection")
    {
    }

    public DbSet<UserProfile> UserProfiles { get; set; }
    public DbSet<Membership> Membership { get; set; }
    public DbSet<Role> Roles { get; set; }
  }

  [Table("UserProfile")]
  public class UserProfile
  {
    [Key]
    [DatabaseGeneratedAttribute(DatabaseGeneratedOption.Identity)]
    public int UserId { get; set; }
    public string UserName { get; set; }
    public string Email { get; set; }
  }

  [Table("webpages_Roles")]
  public class Role
  {
    [Key]
    [DatabaseGeneratedAttribute(DatabaseGeneratedOption.Identity)]
    public int RoleId { get; set; }
    public string RoleName { get; set; }
  }

到目前为止,这是我的控制器:当我单击该角色时,它确实会返回一个在该视图中具有该角色正确名称的视图:

//List all users for a role
[HttpGet]

public ActionResult List(string UserName, string RoleName)
{
    using (UsersContext db = new UsersContext())
    {
        var roleSelect = db.Roles.Where(r => r.RoleName.Equals(RoleName)).FirstOrDefault();

        return View(roleSelect);
    }
}

此视图显示所有已保存角色的列表,以及用于编辑,删除和列出该特定角色的链接。

@{
    ViewBag.Title = "RoleIndex";
    Layout = "~/Views/Shared/_Layout.cshtml";
}

    <div class="spacerBody">
        <h2 class="admin-home-link">@Html.ActionLink("Roles", "AdminIndex")</h2>
        @Html.ActionLink("Create New Role", "RoleCreate") | 
        @Html.ActionLink("Manage User Roles", "RoleAddToUser") 
        <p>&nbsp;</p>
        <div>


            @foreach (string s in Model)
            {

                <div id="userRolesList">
                    <p class="role-p">
                        @s
                    |<span onclick="return confirm('Are you sure to 
                    delete?')">
                       <a href="/Account/RoleDelete?RoleName=@s" 
                     class="delLink"> <span style="color: 
                    #f05322">Delete</span> 
                     </a>
                     </span>
                    |<a href="/Account/Edit?RoleName=@s">Edit</a>   
                    |<a href="/Account/List?RoleName=@s">List</a>

                    </p>
                </div>
                <div>

                </div>

            }
        </div>
    </div>


Then when I click the List link it takes me to this view: 

    @model Comtrex_ICU.Models.Role
    @{
        ViewBag.Title = "List";
        Layout = "~/Views/Shared/_Layout.cshtml";
    }

    <h2 class="admin-home-link">@Html.ActionLink("List", "AdminIndex")</h2>

    <hr/>

    @using (Html.BeginForm())
    {
        @Html.AntiForgeryToken()
        @Html.ValidationSummary(true)
        @Html.HiddenFor(m => m.RoleId)





        <p>
            @Model.RoleName
        </p>
    }


How will i be able to list the specific users that corresponds to the right role?

1 个答案:

答案 0 :(得分:1)

通过使用@Html.TexboBoxFor方法,您要求剃刀引擎渲染该字段的文本框并绑定值。如果您只需要显示字段的文本,只需在p标签中使用模型属性值即可:

<p>
    @Model.RoleName
</p>

更新: 要列出角色列表,可以使用@foreach。有关剃刀语法的更多信息,请查看Microsoft documentation。有一些基本操作和剃刀语法的示例。