用户和角色到 table asp.net mvc
User & roles into a table asp.net mvc
我得到了这个东西,但是我想采用不同的方法,因为当用户只有一个角色时 table 缺少行。我在网站上有 3 个角色:用户、超级用户和管理员。
这是我的控制器:
public ActionResult UserList()
{
var userRoles = new List<RolesViewModel>();
var context = new ApplicationDbContext();
var userStore = new UserStore<ApplicationUser>(context);
var userManager = new UserManager<ApplicationUser>(userStore);
foreach (var user in userStore.Users)
{
var r = new RolesViewModel
{
UserName = user.UserName
};
userRoles.Add(r);
}
foreach (var user in userRoles)
{
user.RoleNames = userManager.GetRoles(userStore.Users.First(s => s.UserName == user.UserName).Id);
}
return View(userRoles);
}
这是我的 RolesViewModel:
public class RolesViewModel
{
public IEnumerable<string> RoleNames { get; set; }
public string UserName { get; set; }
}
和视图:
<table class="table table-hover">
<tr>
<th>
@Html.DisplayNameFor(model => model.UserName)
</th>
<th>
Role 1
</th>
<th>
Role 2
</th>
<th>
Role 3
</th>
</tr>
@foreach (var item in Model)
{
<tr>
<td>
@Html.DisplayFor(modelItem => item.UserName)
</td>
@foreach (var role in item.RoleNames)
{
<td>
@Html.DisplayFor(modelItem => role)
</td>
}
</tr>
}
</table>
所以在 table 我得到角色 1、角色 2 和角色 3 的地方,我想让用户超级用户和管理员分别有一个模型,但我无法真正让它工作,我该如何完成这个最好的方法?
我知道您期望三个特定的角色,但您的模型将来可能会包含更多。在这方面,您的演讲不是很有前瞻性。此外,这种结构分解的方式太多了,无法简洁地回答。相反,我建议您重构网格和视图模型以适应。
稍微更改您的视图模型:
public class RolesViewModel
{
public string RoleNames { get; set; }
public string UserName { get; set; }
}
接下来,结合提高数据访问效率和修改角色 属性 值的构造方式:
public ActionResult UserList() {
var userRoles = new List<RolesViewModel>();
var context = new ApplicationDbContext();
var userStore = new UserStore<ApplicationUser>(context);
var userManager = new UserManager<ApplicationUser>(userStore);
foreach (var user in userStore.Users)
{
var r = new RolesViewModel()
{
UserName = user.UserName,
RoleNames = string.Join(",", userManager.GetRoles(user.Id))
};
userRoles.Add(r);
}
return View(userRoles);
}
然后最后简化您的网格:
<table class="table table-hover">
<tr>
<th>
User Name
</th>
<th>
Roles
</th>
</tr>
@foreach (var item in Model)
{
<tr>
<td>
@Html.DisplayFor(m => item.UserName)
</td>
<td>
@Html.DisplayFor(m => item.RoleNames)
</td>
</tr>
}
</table>
最后,如果您一定要坚持使用这种结构,这里是至少使用 checkbox-style 设置的相同演示文稿:
public class RolesViewModel
{
public string UserName { get; set; }
public bool UserRole { get; set; }
public bool SuperUserRole { get; set; }
public bool AdminRole { get; set; }
}
public ActionResult UserList()
{
var userRoles = new List<RolesViewModel>();
var context = new ApplicationDbContext();
var userStore = new UserStore<ApplicationUser>(context);
var userManager = new UserManager<ApplicationUser>(userStore);
foreach (var user in userStore.Users)
{
var roles = userManager.GetRoles(user.Id);
var r = new RolesViewModel()
{
UserName = user.UserName,
UserRole = roles.Contains("User"),
SuperUserRole = roles.Contains("SuperUser"),
AdminRole = roles.Contains("Admin")
};
userRoles.Add(r);
}
return View(userRoles);
}
}
}
<table class="table table-hover">
<tr>
<th>
User Name
</th>
<th>
User
</th>
<th>
Super User
</th>
<th>
Admin
</th>
</tr>
@foreach (var item in Model)
{
<tr>
<td>
@Html.DisplayFor(m => item.UserName)
</td>
<td>
@Html.CheckBoxFor(m => item.UserRole)
</td>
<td>
@Html.CheckBoxFor(m => item.SuperUserRole)
</td>
<td>
@Html.CheckBoxFor(m => item.AdminRole)
</td>
</tr>
}
</table>
郑重声明,我意识到这个控制器有点难看,因为它通过对每个唯一用户的一系列往返数据存储来获取每个用户角色。如果这个甚至被使用,我会把它留给 OP 来清理...
我得到了这个东西,但是我想采用不同的方法,因为当用户只有一个角色时 table 缺少行。我在网站上有 3 个角色:用户、超级用户和管理员。
这是我的控制器:
public ActionResult UserList()
{
var userRoles = new List<RolesViewModel>();
var context = new ApplicationDbContext();
var userStore = new UserStore<ApplicationUser>(context);
var userManager = new UserManager<ApplicationUser>(userStore);
foreach (var user in userStore.Users)
{
var r = new RolesViewModel
{
UserName = user.UserName
};
userRoles.Add(r);
}
foreach (var user in userRoles)
{
user.RoleNames = userManager.GetRoles(userStore.Users.First(s => s.UserName == user.UserName).Id);
}
return View(userRoles);
}
这是我的 RolesViewModel:
public class RolesViewModel
{
public IEnumerable<string> RoleNames { get; set; }
public string UserName { get; set; }
}
和视图:
<table class="table table-hover">
<tr>
<th>
@Html.DisplayNameFor(model => model.UserName)
</th>
<th>
Role 1
</th>
<th>
Role 2
</th>
<th>
Role 3
</th>
</tr>
@foreach (var item in Model)
{
<tr>
<td>
@Html.DisplayFor(modelItem => item.UserName)
</td>
@foreach (var role in item.RoleNames)
{
<td>
@Html.DisplayFor(modelItem => role)
</td>
}
</tr>
}
</table>
所以在 table 我得到角色 1、角色 2 和角色 3 的地方,我想让用户超级用户和管理员分别有一个模型,但我无法真正让它工作,我该如何完成这个最好的方法?
我知道您期望三个特定的角色,但您的模型将来可能会包含更多。在这方面,您的演讲不是很有前瞻性。此外,这种结构分解的方式太多了,无法简洁地回答。相反,我建议您重构网格和视图模型以适应。
稍微更改您的视图模型:
public class RolesViewModel
{
public string RoleNames { get; set; }
public string UserName { get; set; }
}
接下来,结合提高数据访问效率和修改角色 属性 值的构造方式:
public ActionResult UserList() {
var userRoles = new List<RolesViewModel>();
var context = new ApplicationDbContext();
var userStore = new UserStore<ApplicationUser>(context);
var userManager = new UserManager<ApplicationUser>(userStore);
foreach (var user in userStore.Users)
{
var r = new RolesViewModel()
{
UserName = user.UserName,
RoleNames = string.Join(",", userManager.GetRoles(user.Id))
};
userRoles.Add(r);
}
return View(userRoles);
}
然后最后简化您的网格:
<table class="table table-hover">
<tr>
<th>
User Name
</th>
<th>
Roles
</th>
</tr>
@foreach (var item in Model)
{
<tr>
<td>
@Html.DisplayFor(m => item.UserName)
</td>
<td>
@Html.DisplayFor(m => item.RoleNames)
</td>
</tr>
}
</table>
最后,如果您一定要坚持使用这种结构,这里是至少使用 checkbox-style 设置的相同演示文稿:
public class RolesViewModel
{
public string UserName { get; set; }
public bool UserRole { get; set; }
public bool SuperUserRole { get; set; }
public bool AdminRole { get; set; }
}
public ActionResult UserList()
{
var userRoles = new List<RolesViewModel>();
var context = new ApplicationDbContext();
var userStore = new UserStore<ApplicationUser>(context);
var userManager = new UserManager<ApplicationUser>(userStore);
foreach (var user in userStore.Users)
{
var roles = userManager.GetRoles(user.Id);
var r = new RolesViewModel()
{
UserName = user.UserName,
UserRole = roles.Contains("User"),
SuperUserRole = roles.Contains("SuperUser"),
AdminRole = roles.Contains("Admin")
};
userRoles.Add(r);
}
return View(userRoles);
}
}
}
<table class="table table-hover">
<tr>
<th>
User Name
</th>
<th>
User
</th>
<th>
Super User
</th>
<th>
Admin
</th>
</tr>
@foreach (var item in Model)
{
<tr>
<td>
@Html.DisplayFor(m => item.UserName)
</td>
<td>
@Html.CheckBoxFor(m => item.UserRole)
</td>
<td>
@Html.CheckBoxFor(m => item.SuperUserRole)
</td>
<td>
@Html.CheckBoxFor(m => item.AdminRole)
</td>
</tr>
}
</table>
郑重声明,我意识到这个控制器有点难看,因为它通过对每个唯一用户的一系列往返数据存储来获取每个用户角色。如果这个甚至被使用,我会把它留给 OP 来清理...