How to hide a _Layout.cshtm menu in a speciefic view? - c#

In my MVC 4 project, i have 2 menus, one top fixed menu and a side menu. The top menu is always visible, even when the user isnt logged. But the side menu is only visible when the user is logged. But if the user has a admin role he is able to acces the "Create User" view, and i wish the side menu would not be visible in that speciefic view. How can i do that?
Here´s the side menu part in my layout view:
#if (Request.IsAuthenticated)
{
<div class="row">
<div class="col-md-3">
<p class="lead">Comparações</p>
<div class="list-group">
Item 1
Item 2
</div>
</div>
</div>
}

I used css - you can make this into "HideMenu.cshtml" and call #RenderPage("HideMenu.cshtml") in any cshtml file where you don't want to see the menu.
<style>
body {
padding-top : 0px;
}
.navbar {
display: none;
}
</style>
In this css, "navbar" is the class of the menu item I want to hide.
Use F12 from your browser to find the class name of the element you want to hide.

I use ViewBag to set a flag from the controller. This flag determines whether to do something, but make sure that missing value is also valid and it indicates default action. In that case, only the actions that are affected by the additional part of the layout are free to set the ViewBag item to specific value.
For example, I have a sign in form in layout. But sign in should not be rendered when user visits sign up page, obviously. So, there is a HideSignInForm with default False (and missing equals False). In _Layout.cshtml this is covered by this piece of code:
bool hideSignInForm = false;
if (ViewBag.HideSignInForm != null)
{
hideSignInForm = (bool)ViewBag.HideSignInForm;
}
Later on in the _Layout.cshtml, I use the flag normally:
if (!hideSignInForm)
{
<div id="signInUser">
...
</div>
}

Try:
#if (Request.IsAuthenticated && !HttpContext.Current.User.IsInRole("Admin"))
{
<div class="row">
<div class="col-md-3">
<p class="lead">Comparações</p>
<div class="list-group">
Item 1
Item 2
</div>
</div>
</div>
}

Related

How To Change An Active Tab In DNN From Code-Behind?

On the admin side of my DotNetNuke website, I have two tabs with a few controls. I have also a grid view control on Tab 1 with a few records from the SQL server database i.e. Default Images & Slider Images. Each of the grid view row on Tab 1 also has an Edit button which fills the required textboxes (Image Title, Path, and URL, etc) of Tab 1 with data from that particular row in the code-behind.
When the user clicks on the edit button, an if statement on the code-behind file will check the type of the selected image. If it is a Slider Image, then it's title and URL will be passed to those particular textboxes on Tab 1.
However, if it is a Default Image, then I want to activate Tab 2 and pass the Title, Path, and URL of this default image those specific textboxes on Tab 2.
Is there any way to handle this from c# (code-behind)? Can I mark these tabs something like server controls, so I can easily get them with c# and activate the Tab 2? Any help will highly be appreciated.
Here is an illustration of the Tabs markup I am currently working on.
<div class="dnnForm dnnModuleSettings dnnClear" id="dnnAcProjectsModuleSettings">
<ul class="dnnAdminTabNav dnnClear">
<li>Tab 1</li>
<li>Tab 2</li>
</ul>
<div id="A" class="HomeRoomSlider">
<div class="mspsContent dnnClear">
<fieldset>
//Controls etc
</fieldset>
</div>
</div>
<div id="B" class="HomeRoomSliderDefault">
<div class="mspsContent dnnClear">
<fieldset>
//Controls etc
</fieldset>
</div>
</div>

MVC 6 Best way to send parameters to view

I would like to know what is the best way to send a parameter to a view.
This is my first time with MVC.
I have a menu and I would like to change the item menu class depending which item menu is selected.
For example I have a menu with Home, Products, About.
<div class="nav-main-item">
<a asp-controller="Home" asp-action="About">
<div class="top-solid-line selected"></div>
<div class="row nav-item">
<div class="item-line-1">
Home
</div>
</div>
</a>
</div>
<div class="nav-main-item">
<a asp-controller="Home" asp-action="Products">
<div class="top-solid-line"></div>
<div class="row nav-item">
<div class="item-line-1">
Products
</div>
</div>
</a>
</div>
<div class="nav-main-item">
<a asp-controller="Home" asp-action="About">
<div class="top-solid-line"></div>
<div class="row nav-item">
<div class="item-line-1">
About
</div>
</div>
</a>
</div>
If I select Products I want to add the class 'selected' to it, and remove the class selected to the item menu was selected before an so on.
I know how to do it in jquery, is very simple, but the thing is when I click one of the items menu the selected class continue in the home item menu, that is because I initialize the selected on the Home item menu.
I need some kind of parameter to pass to the view and depending on that value add the class selected to the item menu selected.
I know should be very simple, any help will be appreciate it!
Thanks.
In the controller you can use return View(model), where model is some variable or object. In the cshtml file you can then declare #Model int for instance, when passing an integer and access it like a normal variable as model. For instance: #if (model == 1){ your code here }.
EDIT: you can also add parameters to the ViewBag (google it, there are lots of examples) but I would use the model since it is strongly typed.
ASP.NET MVC Send Multiple Data to View
My opinion
ViewBag, ViewData,TempData is basic solution method exm: (alert, message, script vb. other run client-side code and single data) but write a long code line this dirty and complexible. But using ViewModel property is best solve so your code is not complexible and full compatible Object Oriented Programming architecture.
Understanding ViewModel

Show/hide item in "_layout"

I'm working on a C# MVC project and I want a master page that has a header that contains some information among which is the application logo on the top left corner. However, there's one view in which I don't want it to be seen, the login page.
So, how can I hide it for just the Home/Index view and then display it for all the other views?
Here's part of the _Layout.cshtml
...
<body>
<header>
<div class="content-wrapper">
<div class="float-left">
<img src="../../Images/_logo.png" alt="whatever" />
</div>
...
This shows in every page, I just wish I could hide it from the initial page where the user enters its credentials.
How can this be achieved?
Thanks
You can set Layout = null for that Login page.
#{
Layout = null;
}
You can use section of MVC views for this purpose. They are precisely used for this -
in _layout.cshtml include header section as this, so that the header is included as an optinal section
#RenderSection("Header", False)
#RenderSection("MainContent")
then other pages you can have separate header such as this (some.cshtml) -
#section Header {
<header>
<div class="content-wrapper">
<div class="float-left">
<img src="../../Images/_logo.png" alt="whatever" />
</div>
}
#section MainContent{
other body content.
}
And for login just don't define the section and only provide the body -
#section MainContent{
other body content.
}
This gives you not only the option to show hide header but also a great option to customize your layout based on the contents and need of the content pages.
You can learn more from here -
http://www.codeproject.com/Articles/383145/RenderBody-RenderPage-and-RenderSection-methods-in
and in msdn you can find the Documentation
http://msdn.microsoft.com/en-us/library/system.web.webpages.webpagebase_methods(v=vs.111).aspx
You can check to see if the current user is authenticated, and if not then hide the elements you wish to hide.
#if(HttpContext.Current.User != null && HttpContext.Current.User.Identity != null && HttpContext.Current.User.Identity.IsAuthenticated)
{
... stuff to show if user is authenticated here ...
}
You can write a JQuery on your login page (cshtml). Use $(document).ready() event and hide the div containing the logo image.
use script
$(window).load(function () {
$("#actionlinkid").hide = true;
});
Agree with solution given by Softsen. But in case you are asking only for hiding logo for particular page, You can do it as below:
#if(Request.Url.ToString().IndexOf("index")==-1)
{
<div class="float-left">
<p class="site-title">#Html.ActionLink("your logo here", "Index", "Home")</p>
</div>
}

umbraco 7: get property value

I am using umbraco 7 ASP.NET C# MVC 4.
Im trying to use the new umbraco, so far its been ok, but i need to get a property of one of my pages i set up.
#inherits Umbraco.Web.Mvc.UmbracoTemplatePage
#{
var homePage = CurrentPage.AncestorsOrSelf(1).First();
var menuItems = homePage.Children.Where("isMenu == true");
}
<!-- Nav -->
<div class="row">
<div class="col-md-12 top-menu-container">
<ul>
#* If the Url of the current page is "/" then we want to add the class "current_page_item" *#
#* Otherwise, we set the class to null, that way it will not even be added to the <li> element *#
<li>
<div onclick="location.href='/';" class="col-md-2 metro-container" style="background-color:##Model.Content.GetPropertyValue("navigationColor")" >
<img class="menu-img" src="../../media/1001/home.png" alt="" />
Home
</div>
</li>
#foreach (var item in menuItems)
{
#* If the Id of the item is the same as the Id of the current page then we want to add the class "current_page_item" *#
#* Otherwise, we set the class to null, that way it will not even be added to the <li> element *#
<li>
<div onclick="location.href='#item.Url';" class="col-md-2 metro-container" style="background-color:##item.Content.GetPropertyValue("navigationColor")" >
<img class="menu-img" src="../../media/1001/home.png" alt="" />
#item.Name
</div>
</li>
}
</ul>
</div>
</div>
So the first "li" outside the loop i simply get the models content Getproperty method which surely enough gets me any property i tell it too.
However the loop i have although goes through the current pages children, i cant seem to get a specific property.
Makes it worse is intellicense Isn't working as its all runtime.
The line in question is
<div onclick="location.href='#item.Url';" class="col-md-2 metro-container" style="background-color:##item.Content.GetPropertyValue("navigationColor")" >
I need to get the color that was set on the page from the navigationColor control.
What am i doing wrong here?
Big derp on my side.
The item var in the foreach loop already has property's contained in the item.
all you need to do is
item.NameofYourPropertyAlias
and that should do it.
The following code works for me:
item[index].GetProperty("name").Value
Makes it worse is intellicense Isn't working as its all runtime.
The problem is that you're using dynamic model instead of the strongly typed kind.
Instead of using CurrentPage you can use Model.Content.
So instead of:
var homePage = CurrentPage.AncestorsOrSelf(1).First();
Use:
var homePage = Model.Content.AncestorOfSelf(1);
Intellisense will then work after you type a stop after homePage.
I would also strongly recommend inputting using Umbraco.web with the rest of your using statements at the top of the template. This should further improve intellisense, and allow you to see methods such as .GetPropertyValue<T>(string alias).
You should then be able to use item.GetPropertyValue<string>("navigationColor").
item.NameofYourPropertyAlias
this is the proper use of item while looping

JQuery Tabs in asp.net to stay on reload

i'm having a JQuery tab on my page and currently when i reload or run some serverside codes , the tab would go back to the first one, is there a way to make the tabs stay?
I would like my tab to stay on the current selected tab rather than go back to the first tab when the page reloads
My JQuery:
$(function () {
var tabContainers = $('div.tabs > div');
$('div.tabs ul.tabNavigation a').click(function () {
tabContainers.hide();
tabContainers.filter(this.hash).show();
$('div.tabs ul.tabNavigation a').removeClass('selected');
$(this).addClass('selected');
return false;
}).filter(':first').click();
});
My tabs:
<div class="tabs">
<ul class="tabNavigation">
<li>Annoucments</li>
<li>Events</li>
<li>Photos</li>
<li>Job Opportunities</li>
<li>Contacts</li>
<li>Videos</li>
</ul>
<div id="Annoucments" class="ContentDIV">
..
</div>
<div id="Events" class="ContentDIV">
..
</div>
<div id="Photos" class="ContentDIV">
..
</div>
<div id="JobOpportunities" class="ContentDIV">
..
</div>
<div id="Contacts" class="ContentDIV">
...
</div>
<div id="Videos" class="ContentDIV">
..
</div>
</div>
You need to save user state somewhere:
Url - parse url and determine whether it's yours tab secion or not.
Cookies - write to cookie user information (e.g. selected tab).
Session - same as for cookie.
Custom Hidden field or asp.net ViewState.
One thing you can try is to keep a hidden field that maintains the href value of the currently selected tab. This way, when there is a postback, you can read the value from that hidden field and select the correct tab.
Put a get parameter in your page url. So you could have yourpage.aspx?page=1 for example. Then just pull that parameter out of the querystring and either apply the 'selected' class in your server side code (which would be best) or in jQuery on the page directly (not as good a solution but it'll work too) to the appropriate tab.
EDIT (Code Sample)
$('#[formID]').onSubmit(function () {
var tab = $('.selected').prop('href');
// Append a hidden field to the form containing tab. Example below
return true;
}
...and then when you load your page from the server, simply look at the value passed and apply the 'selected' class to the element.
Example:
jQuery - add additional parameters on submit (NOT ajax)

Categories

Resources