我有一个WebMatrix 2.0应用程序,我想拥有用户的角色 在_SiteLayout.cshtml页面的导航区域内。
作为我在其他地方删除角色的例子,我有这个:
@{
PageData["Title"] = "Agma App: Users and Roles ";
.
.
.
// Errors
var lstErrors = new List<string>();
lstErrors.Clear();
var result = "";
//Code to remove a role(s)
var deleteRoleNames = Request.Form.GetValues("deleteRoleName");
if(deleteRoleNames != null){
foreach(var r in deleteRoleNames)
{
try
{
Roles.DeleteRole(r);
}
catch (Exception ex)
{
@Html.ValidationMessage("deleteRoleName")
lstErrors.Add("Role is not empty, it has users associated with it.");
lstErrors.Add(ex.Message);
result = "Role is not empty, it has users associated with it.";
}
}
if(lstErrors.Count == 0){
}
else{
PageData["errors"] = lstErrors;
}
}
}
<h1>@PageData["Title"]</h1>
@Html.ValidationSummary()
<form action="" method="post">
<fieldset>
<legend>Existing Roles</legend>
<ul class="roles">
@foreach(var r in roles){
<li>
<label for="deleteRoleName"><input type="checkbox" name="deleteRoleName" value="@r "/> @r</label>
</li>
}
</ul>
<input type="submit" value="Delete Checked Roles" onclick="return confirm('Are you sure you want delete the checked roles?');"/>
</fieldset>
</form>
This works fine.
Back to _SiteLayout -- my <nav> area is currently compensating for my lack of knowledge by hard- coded references to roles, i.e.:
<nav>
<ul id="menu">
<li><a href="~/">Home</a></li>
@if (User.IsInRole("Administrator")) {
<li><a href="~/Administrator/Home">Administrator</a></li>
}
@if (User.IsInRole("BoardMember")) {
<li><a href="~/">BoardMember</a></li>
}
.
.
.
<li><a href="~/About">About</a></li>
<li><a href="~/Contact">Contact</a></li>
</ul>
</nav>
我的问题是,因为_SiteLayout不是以@ {}结构开头的,所以我对如何完成我想做的事情感到不满。