您好,
我在该页面中有一页我必须展示一些细节 用户数据。如果我使用admin角色登录,那么所有细节都是 可见,我使用用户角色登录一些细节将是非 可见
供你参考我添加了图片: -
控制器: -
public ActionResult EditableUserDetails()
{
ViewBag.UserName = "Welcome" + ":" + " " + Session["UserName"].ToString();
ViewBag.UserTypeId = Session["UserTypeId"].ToString();
//List<EditableUserDetails> EditableUserDetailsobject = new List<EditableUserDetails>();
var linq = (from db in EntityObj.Users
where db.IsActive == true
select new EditableUserDetails
{
UserId = db.UserId,
UserName = db.UserName,
Password = db.Password,
Category = db.Category
}).ToList();
var data = linq.ToList();
return View(data);
}
模特课: -
public class EditableUserDetails
{
public string UserName { get; set; }
public int UserId { get; set; }
public string Password { get; set; }
public string Category { get; set; }
}
如果我点击“人员”框,它将显示所有用户的详细信息,但我必须仅为管理员角色而不是用户角色设置此权限。
是否有任何实施方法取决于登录详细信息。 谢谢。
查看页面: -
@model IEnumerable<stg_test2.Models.EditableUserDetails>
@{
Layout = "~/Views/Shared/_Layout.cshtml";
@Scripts.Render("~/bundles/jqueryui")
}
<input type="submit" value="Person" id="sbtPersonDetails" />
<input type="submit" value="Volunteer" id="sbtVolunteerDetails" />
<input type="submit" value="Potential Volunteer" id="sbtPotentialVolunteerDetails" />
<input type="submit" value="Child Sponcers" id="sbtChildSponcerDetails" />
<input type="submit" value="Children Details" id="sbtChildren" />
<input type="submit" value="Year Review Report" id="sbtYearReviewReport" />
<table id="tblUsers">
<thead>
<tr style="background-color: #7ac0da;">
<th>UserName</th>
<th>Password</th>
<th>Category</th>
<th class="td-img">Process</th>
</tr>
</thead>
<tbody>
@foreach (var @item in Model)
{
<tr>
<td>@item.UserName</td>
<td>@item.Password</td>
<td>@item.Category </td>
<td class="td-img">
<img src="../Images/Edit.png" class="imgEdit" /><img src="../Images/Trash.png" class="imgDelete" /></td>
<td>
<input type="hidden" value="@item.UserId" class="hdnUserId"/></td>
</tr>
}
</tbody>
</table>
答案 0 :(得分:1)
您尚未显示您的观看代码,但我将取消您展示的内容。
首先,我建议将您的域模型与视图模型分开。这会创建关注点分离,允许您修改视图模型而不影响域模型(反之亦然),并允许您仅发送视图所需的属性。
创建一个这样的视图模型:
public class EditableUserDetailsViewModel
{
public bool IsAdmin { get; set; }
// Include other properties from EditableUserDetails, but only those that are needed for the view to consume
}
然后在你的控制器中:
public ActionResult EditableUserDetails()
{
ViewBag.UserName = "Welcome" + ":" + " " + Session["UserName"].ToString();
ViewBag.UserTypeId = Session["UserTypeId"].ToString();
EditableUserDetails domainModel = (from db in EntityObj.Users
where db.IsActive == true
select new EditableUserDetails
{
UserId = db.UserId,
UserName = db.UserName,
Password = db.Password,
Category = db.Category
}).ToList();
EditableUserDetailsViewModel viewModel = new EditableUserDetailsViewModel {
IsAdmin = ..., // Set this boolean value based on the user's role
// Fill in other properties needed for the view from the domain model above
};
return View(viewModel);
}
(我也会问你为什么要在会话中存储欢迎信息,但这超出了这个问题和答案的范围。)
在您看来,仅当IsAdmin
为真时才显示仅限管理员的部分:
@if (Model.IsAdmin) {
<div>Your Person here</div>
}
答案 1 :(得分:0)
嗯,不确定这是否是你想要的,但你可以在你的视图上做到这一点:
@if(Roles.IsUserInRole(userName, role))
{
//display the div
}
else
{
//don't display the div
}
这将检查用户是否匹配查看某些内容所需的角色,然后相应地设计视图。