C# 如何基于当前用户登录在视图中显示某些元素
我有两种类型的用户角色,例如:用户和管理员。 当登录角色为用户时,我想显示C# 如何基于当前用户登录在视图中显示某些元素,c#,asp.net-mvc,C#,Asp.net Mvc,我有两种类型的用户角色,例如:用户和管理员。 当登录角色为用户时,我想显示Update链接,但当角色为Admin nUpdate和delete时,这两个链接都应该出现在视图模块中 我怎样才能做到这一点?这是我的看法。其中我想根据用户角色设置更新和删除的链接 @model List<WebApplication5.Models.MyModel> @{ Layout = null; } <!DOCTYPE html> <html> <head&
Update
链接,但当角色为Admin nUpdate
和delete
时,这两个链接都应该出现在视图
模块中
我怎样才能做到这一点?这是我的看法。其中我想根据用户角色设置更新
和删除
的链接
@model List<WebApplication5.Models.MyModel>
@{
Layout = null;
}
<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width" />
<title>Show</title>
</head>
<body style="background-color:aliceblue">
<div>
@TempData["Message"]
<b><center>Data Of Table</center></b>
<table style="background-color:azure" border="1" align="center" title="DATA OF EMPLOYEE">
@foreach (var objmymodel in Model)
{
<tr>
<td>@objmymodel.Username</td>
<td>@objmymodel.Password</td>
<td>@Html.ActionLink("delete", "Delete", new {id=@objmymodel.Id })</td>
<td>@Html.ActionLink("update", "Update", new { id = @objmymodel.Id })</td>
</tr>
}
</table>
</div>
<div>@Html.ActionLink("Logout","Login","Mycontroller")</div>
</body>
</html>
这是控制器的代码
public ActionResult Login()
{
return View();
}
[HttpPost]
public ActionResult Login(MyModel objmymodel)
{
ado.Logindata(objmymodel);
TempData["Message"] = ado.message;
if (ado.message == "FAIL")
{
objmymodel.Username = "";
objmymodel.Password = "";
return RedirectToAction("Login");
}
else
{
TempData["Message"] = ado.usernamestring;
return RedirectToAction("Show");
}
}
在这种情况下,您必须执行以下任务 第一种方法
public class MyModel
{
// Your other props.
public bool HasAdminAccess { get; set; }
}
if (objmymodel.Username == "admin" && objmymodel.Password == "admin") {
usernamestring = "admin";
message = "welcome " + objmymodel.Username;
objmymodel.HasAdminAccess = true;
}
@if (Model.HasAdminAccess)
{
<td>@Html.ActionLink("delete", "Delete", new {id = @objmymodel.Id})</td>
}
<td>@Html.ActionLink("update", "Update", new { id = @objmymodel.Id })</td>
控制器/操作:
public class MyModel
{
// Your other props.
public bool HasAdminAccess { get; set; }
}
if (objmymodel.Username == "admin" && objmymodel.Password == "admin") {
usernamestring = "admin";
message = "welcome " + objmymodel.Username;
objmymodel.HasAdminAccess = true;
}
@if (Model.HasAdminAccess)
{
<td>@Html.ActionLink("delete", "Delete", new {id = @objmymodel.Id})</td>
}
<td>@Html.ActionLink("update", "Update", new { id = @objmymodel.Id })</td>
查看:
public class MyModel
{
// Your other props.
public bool HasAdminAccess { get; set; }
}
if (objmymodel.Username == "admin" && objmymodel.Password == "admin") {
usernamestring = "admin";
message = "welcome " + objmymodel.Username;
objmymodel.HasAdminAccess = true;
}
@if (Model.HasAdminAccess)
{
<td>@Html.ActionLink("delete", "Delete", new {id = @objmymodel.Id})</td>
}
<td>@Html.ActionLink("update", "Update", new { id = @objmymodel.Id })</td>
@if(Model.HasAdminAccess)
{
@ActionLink(“delete”,“delete”,new{id=@objmymodel.id})
}
@ActionLink(“更新”,“更新”,新的{id=@objmymodel.id})
*
当您第一次请求(GET)HasAdminAccess时,将有false和delete
按钮将不会被渲染