C# 单击该项后显示数据库中的特定数据
如何列出已分配给某个角色的所有用户。这是我的模型C# 单击该项后显示数据库中的特定数据,c#,asp.net-mvc-4,model-view-controller,roles,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 DbS
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; }
}
此视图显示保存的所有角色的列表,其中包含编辑、删除和列出该特定角色的链接
@{
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> </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?
@{
ViewBag.Title=“RoleIndex”;
Layout=“~/Views/Shared/_Layout.cshtml”;
}
@ActionLink(“角色”、“管理员索引”)
@ActionLink(“创建新角色”、“角色创建”)|
@ActionLink(“管理用户角色”、“角色扮演者”)
@foreach(模型中的字符串s)
{
@
|
|
|
}
然后,当我单击列表链接时,它会将我带到该视图:
@模型Comtrex_ICU.Models.Role
@{
ViewBag.Title=“列表”;
Layout=“~/Views/Shared/_Layout.cshtml”;
}
@ActionLink(“列表”、“管理员索引”)
@使用(Html.BeginForm())
{
@Html.AntiForgeryToken()
@Html.ValidationSummary(true)
@Html.HiddenFor(m=>m.RoleId)
@罗勒烯模型
}
如何列出与正确角色对应的特定用户?
通过使用@Html.texboxfor
方法,您要求razor引擎为该字段呈现文本框并绑定值。如果只需要显示字段的文本,只需在p
标记中使用模型属性值:
<p>
@Model.RoleName
</p>
@罗勒烯模型
更新:
要列出角色列表,可以使用
@foreach
。有关razor语法的更多信息,请查看。这里有一些基本操作和razor语法的示例。谢谢。成功了。您知道如何显示与正确角色名相关的用户名列表吗?
<p>
@Model.RoleName
</p>