asp.net-mvcasp.net-mvc-partialview

Hide Register and Log In links in _LoginPartial.cshtml based on Current View MVC 5


I want to hide the Register and Log In links when a new user is registering. This is the View that says "Registration is Almost complete, please check your emails and confirm email address".

Below is the "else statement" for unauthorized users. If I can determine the View _LoginPartial is inserted into, I can hide the links for a registering user.

Currently:

else
{
    <ul class="nav navbar-nav navbar-right" style="background-color:darkblue;">
        <li>@Html.ActionLink("Register", "Register", "Account", routeValues: new { @returnUrl = "/StoreMaster/Stores/PickLocation" }, htmlAttributes: new { id = "registerLink", style = "color:white;" })</li>


        <li>@Html.ActionLink("Log in", "Login", "Account", routeValues: new { @returnUrl ="/StoreMaster/Stores/PickLocation"}, htmlAttributes: new { id = "loginLink", style = "color:white;" })</li>
    </ul>
}

I would like to put: "if View== thisViewName then hide these links"


Solution

  • You can get the name in the View and then use it in a condition as shown below:

    @{
        var  pageName = ViewContext.RouteData.Values["Action"].ToString();
    }
    
    @if (pageName != "DisplayEmail")
    {
        <div class="navbar-collapse collapse" style="background-color:darkblue;">
    
        <ul class="nav navbar-nav">
            <li>@Html.ActionLink("Latest", "Latest", "Stores", routeValues: null, htmlAttributes: new { @style = "color:white;" })</li>
            <li>@Html.ActionLink("HowTo", "HowTo", "Shuttles", routeValues: null, htmlAttributes: new { @style = "color:white;" })</li>
            <li>@Html.ActionLink("Contact Us", "ContactUsPublic", "Stores", routeValues: null, htmlAttributes: new { @style = "color:white;" })</li>
          @*<li>@Html.ActionLink("Pick something", "PickSomething", "Stores", routeValues: null, htmlAttributes: new { @style = "color:white;" })</li>*@
        </ul>
        }
    
        @Html.Partial("_LoginPartial")
                 
        </div>
    }