Asp.Net Mvc布局和部分视图

时间:2017-05-28 10:17:19

标签: asp.net-mvc layout asp.net-mvc-partialview

那里......我对Layout和PartialLayout感到很困惑。 我不知道我在做什么错。 当我以管理员身份登录时,我可以查看所有链接,甚至此管理员链接(“注册”,“角色”,“部门”和“管理”)以及注销链接。到现在为止还挺好。 但是,当我点击管理员链接的任何链接,然后我的布局发生变化,因为我不是管理员,我的意思是我只看到“主页”,“关于”“kontakt”并且非常混乱我的登出链接更改为登录后所有我登录。所以我记录了,但显示我登录。 然后,如果我点击“主页”..或关于..然后我再次看到所有链接和注销链接。 请给我这个工作人员的新手,并通过编码告诉我它是否正常,因为我无法在很长时间内解决这个问题......

这是我的一个管理员html“角色”

@model IEnumerable<MyApiDemo.Models.RoleViewModel>

@{
    ViewBag.Title = "Index";
}

<h2>Index</h2>

<p>
    @Html.ActionLink("Create New", "Create")
</p>
<table class="table">
    <tr>
        <th>
            @Html.DisplayNameFor(model => model.Name)
        </th>
        <th></th>
    </tr>

@foreach (var item in Model) {
    <tr>
        <td>
            @Html.DisplayFor(modelItem => item.Name)
        </td>
        <td>
            @Html.ActionLink("Edit", "Edit", new { id=item.Id }) |
            @Html.ActionLink("Details", "Details", new { id=item.Id }) |
            @Html.ActionLink("Delete", "Delete", new { id=item.Id })
        </td>
    </tr>
}

</table>

在我的Index.cshtml中,包括_Layout。 这是我的_Layout.cshtml

<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>@ViewBag.Title</title>
    @Styles.Render("~/Content/css")
    @Scripts.Render("~/bundles/modernizr")



</head>
<body>
    <div class="navbar navbar-inverse navbar-fixed-top">
        <div class="container">
            <div class="navbar-header">
                <button type="button" class="navbar-toggle" data-toggle="collapse" data-target=".navbar-collapse">
                    <span class="icon-bar"></span>
                    <span class="icon-bar"></span>
                    <span class="icon-bar"></span>
                </button>
                @Html.ActionLink("Application namn", "Index", "Home", new { area = "" }, new { @class = "navbar-brand" })
            </div>
            <div class="navbar-collapse collapse">
                <ul class="nav navbar-nav">
                    <li>@Html.ActionLink("Home", "Index", "Home")</li>
                    <li>@Html.ActionLink("About", "About", "Home")</li>
                    <li>@Html.ActionLink("Contact", "Contact", "Home")</li>



                    @Html.Partial("_LoginPartial")
                </ul>


            </div>
        </div>
    </div>
    <div class="container body-content">
        @RenderBody()
        <hr />
        <footer>
            <p>&copy; @DateTime.Now.Year</p>
        </footer>
    </div>

    @Scripts.Render("~/bundles/jquery")
    @Scripts.Render("~/bundles/bootstrap")
    @RenderSection("scripts", required: false)
</body>
</html>

这是我的_LoginPartial.cshtml

@using Microsoft.AspNet.Identity
@if (Request.IsAuthenticated && ViewData.ContainsKey("FullName"))
{
    using (Html.BeginForm("LogOff", "Account", FormMethod.Post, new { id = "logoutForm", @class = "navbar-right" }))
    {

        @Html.AntiForgeryToken()

        <ul class="nav navbar-nav navbar-right">

            @if (HttpContext.Current.User.IsInRole("Admin"))
            {
                <li>@Html.ActionLink("Register", "Register", "Account", routeValues: null, htmlAttributes: new { id = "registerLink" })</li>
                <li>@Html.ActionLink("Role", "Index", "Role")</li>
                <li>@Html.ActionLink("Department", "Index", "Department")</li>
                <li>@Html.ActionLink("Manage", "Pass", "Account")</li>
            }

            <li>

                @Html.ActionLink("Welcome back " + (ViewData["FullName"]) + "!", "Index", "Manage", routeValues: null, htmlAttributes: new { title = "Manage" })

            </li>
            <li><a href="javascript:document.getElementById('logoutForm').submit()">Logout</a></li>

        </ul>
    }
}
else
{
    <ul class="nav navbar-nav navbar-right">

        <li>@Html.ActionLink("Login", "Login", "Account", routeValues: null, htmlAttributes: new { id = "loginLink" })</li>
    </ul>
}

0 个答案:

没有答案