0

there.. I'am really confused about Layout and PartialLayout. I don't know what wrong I'am doing. When I login as Admin I can se all links even this Admin links ("Register", "Role", "Department" and "Manage") plus Logout link. So far so good. But when I click any link of Admin links then my Layout changes as I'am not Admin I mean I see only "Home" , "About" "kontakt" ANd very confusing my logout link changes to Login after all I'am logged. So I'am logged but shows me Login instead. Then if I click "Home"..or About.. then I see all links again and Logout link. Please I'am new to this staffs and show me by coding if it's Ok, becouse I couldn't fix this problem in many hours ...

Here is my one of the admin html "Role"

@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>

In my Index.cshtml , _Layout is included. Here is my _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>

And this is my _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>
}
4
  • Does ViewData contain a value for FullName? (and why are you using that condition anyway?) Commented May 28, 2017 at 10:29
  • @StephenMuecke Thank you for your response. Yes ViewData contain value and I can see my Full name. I use condition becouse I have to kind of users. I don't want to show Role, Register..to all, only to Admins. I have tried to create two kind of Layout Views but when it comes to _LoginPartial.. then it goes wrong. Now I have added my Role.html file too to my code. Commented May 28, 2017 at 11:02
  • But you claiming you cant see it because the code in the else block is generated - i.e. your Login link, not the LogOff. And what do you mean you have created two kind of Layout Views - you have not (your _LoginPartial is a partial view, not a layout) Commented May 28, 2017 at 11:06
  • @StephenMuecke I mean I tried with two different Layout Views. Commented May 28, 2017 at 13:04

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.