【问题标题】:Display Specific Data from Database after click that item单击该项目后显示数据库中的特定数据
【发布时间】:2019-08-02 00:28:42
【问题描述】:

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

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?

【问题讨论】:

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


    【解决方案1】:

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

    <p>
        @Model.RoleName
    </p>
    

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

    【讨论】:

    • 谢谢。那行得通。你知道如何显示对应正确角色名称的用户名列表吗?
    猜你喜欢
    • 2013-04-28
    • 1970-01-01
    • 1970-01-01
    • 2018-03-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多