Defining a section for all views - c#

I've created a section for a footer in my asp.net MVC 3 Web Application:
<footer>
#RenderSection("Footer", true)
</footer>
This footer will be the same on every page, so it doesn't make sense for me to define it for each and every view. So, is there any way I can globally declare this footer section for all views? The footer will contain code so as far as I know it's bad practice, if not impossible, to directly define it in the .cshtml file.
Thank you in advance.

I have handled the same scenario by creating a partial view "_Footer" and place it on the "_Layout".
#ViewBag.Title
#Html.Partial("_Header")
<div id="content">
<div id="nav-bar">
#Html.Partial("_Menu")
</div>
<div class="container">
#RenderBody()
</div>
</div>
<div id="footer">
#Html.Partial("_Footer")
</div>
#Html.Partial("_Scripts")

Sure:
<footer>
#if (IsSectionDefined("footer"))
{
#RenderSection("footer")
}
else
{
... put your default footer here
}
</footer>
And in views that you want to override the footer simply define the section.

You can place the footer in your SiteLayout.cshtml. See this article for more information on using layouts with MVC 3.

Related

.cshtml Razor Code Template (.NET)

I just started working with, NET and I'm not very familiar with razor code.
I want to remove redundancy in my razor (.Net) code, without a big mess. I have about a dozen, PDF generating .cshtml files with razor code in it and the number is growing. Each file is nearly the same. The whole structure is always the same, only the content is always different. Because the files are pretty big, I will make an abstract example (ignore the real meaning of the elements, the list could be a different div for example):
File 1:
<div>
<p>
#content
</p>
</div>
<ul>
<li>
<a>
#comment
</a>
</li>
</ul>
File 2:
<div>
<p>
#picture
</p>
</div>
<ul>
<li>
<a>
<div>
#link
#metadata
</div>
</a>
</li>
</ul>
I thought it's not very clean to use #helper or #function for the different sections that are the same. The code would be not very clear to read with 10+ functions in it, that is only for the structure there.
I cant either just make a function to insert the value because sometimes there is, for example, a link or an additional div element inside the main structure.
At the end I want to have one place where I can change the basic structure, to change all structures, without going through 10+ files and change them one by one.
Is it possible to make such a "template"?
You can use Layout pages that define the overall structure. "Content", "Comment", etc are defined as sections in the layout, these sections get their content from the cshtml file that uses the layout. Creating a Consistent Layout in ASP.NET Web Pages (Razor) Sites.
_Layout.cshtml
<div>
<p>
#if (IsSectionDefined("content")) {
#RenderSection("content", false)
}
</p>
</div>
<ul>
<li>
<a>
#if (IsSectionDefined("comment")) {
#RenderSection("comment", false)
}
</a>
</li>
</ul>
.cshtml that uses this layout
#{ Layout = "~/Views/Shared/_Layout.cshtml"; }
#section content {
<div>My content</div>
}
#section comment {
bla bla bla ...
}

Prevent NavBar showing

I have a C# MVC application with a pretty-much standard Bootstrap display.
As usual the Navbar is provided by Shared\_Layout.cshtml.
However one of my pages will only be used for generating a PDF using an HTML/PDF converter, so I don't want the Navbar to appear.
Is there a way to prevent this for specific pages?
I realised all I had to do was to add a querystring parameter that I can check for in _Layout.cshtml, i.e.:
<body>
#if(Request.QueryString["noheader"] == null)
{
<div class="no-print navbar navbar-inverse navbar-fixed-top">
<div class="container">
<div class="navbar-header">
...

MVC - Nested Partial Views

I am getting an error ("Server Error in '/' Application") when I nest partial views in my MVC app. The views work fine individually, but not when nested. It's my understanding that it's okay to do this, so what am I doing wrong?
Essentially, I am trying to use partials as sub-layouts within my _Layout.cshtml.
Here's my main layout - _Layout.cshtml
<!DOCTYPE html>
<html>
<head>...</head>
<body style="padding-top: 80px;">
<div class="container-fluid">
<div class="row">
<div id="myTab" class="col-lg-12 col-md-12 col-sm-12">
...
<div class="tab-content">
<div class="tab-pane fade" id="search">
#Html.Partial("~/Areas/Search/Views/Shared/_SearchLayout.cshtml")
</div>
</div>
</div>
</div>
</div>
#RenderBody()
#RenderSection("scripts", required: false)
</body>
</html>
This is the first partial view (_SearchLayout). If I remove the partials AND #RenderBody, no error.
<div class="container-fluid">
#Html.Partial("_PolicySearch")
#Html.Partial("_ClaimSearch")
</div>
#RenderBody()
This partial view is nested in the first partial view (_SearchLayout):
<div class="row top-buffer search-outline form-horizontal">
<div class="col-md-1 search-icon-size text-primary">
<i class="glyphicon glyphicon-heart"></i>
</div>
<div class="col-md-1 search-icon-size text-primary">
<h4>Claim Search</h4>
</div>
</div>
In your first partial view:
Remove RenderBody
Replace Html.Partial to Html.RenderPartial
I also would recommend to rename your partial view to something not containing the word "Layout" to avoid the mismatch between the view types.
Use Html.RenderPartial instead
The problem is #RenderBody(). This can only be called in a layout, which when used in this way _SearchLayout.cshtml is not, despite its name.
The important thing to remember about layouts, partials and views in ASP.NET MVC is that they're all views. The only thing that differentiates them is how they're used. In this instance, you're using the _SearchLayout.cshtml view as a partial, and partials can't use #RenderBody().

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

Nested layouts for MVC5

I've seen a few posts on this topic:
Razor Nested Layouts with Cascading Sections
MVC 3 - Nested layouts - sections don't render in Areas
And it always seems to be problematic. However they are both pretty old so wondering if things have changed.
Basically I have a master layout, and 3 different body templates based on what kind of page it is. For examples sake:
_Layout.cshtml
<html lang="en">
<head>
</head>
<body style="padding: 50px 0;">
<header class="navbar navbar-default navbar-fixed-top" role="banner">
#Html.Partial("_MenuPartial")
</header>
<ol class="breadcrumbs">
#RenderSection("breadcrumbs", true);
</ol>
<section>
#RenderBody();
</section>
<footer class="navbar navbar-default navbar-fixed-bottom">
#Html.Partial("_FooterPartial")
</footer>
#Html.Partial("_ScriptInitPartial")
</body>
</html>
_LayoutForEdit.cshtml
<div class="panel panel-primary">
<div class="panel-body">
<div class="col-lg-2">
<ul class="nav nav-pills nav-stacked">
#RenderSection("tabs", true)
</ul>
</div>
<div class="col-lg-10">
<div class="tab-content">
#RenderBody()
</div>
</div>
</div>
<div class="panel-footer">
<button class="btn btn-primary" data-bind="enable: Entity.isValid, click: save">Save</button>
</div>
</div>
Now this renders fine when called. Almost.
The rendering of sections must be in the child layout it seems. If I try to put the breadcrumbs in the _Layout.cshtml, it will fail because _LayoutForEdit.cshtml never rendered it. How can I fix this?
The following sections have been defined but have not been rendered for the layout page "~/Views/Shared/_LayoutForEdit.cshtml": "breadcrumbs".
I know it's an old question. I thought I'd share this anyway in case anyone else runs into this (like I did).
At the bottom of your child layout, you define a section with the same name as the section in the parent layout. Inside of this section you simply put a #RenderSection, again specifying the same name as before. Once this is in place, you essentially have the child layout "bypass" content from pages, up to its parent layout:
#section breadcrumbs {
#RenderSection("breadcrumbs", true)
}
Not sure if you still need help, but I'll answer anyways.
There RenderSection method takes the following parameters according to the
MSDN Documentation:
public HelperResult RenderSection(
string name,
bool required
)
Parameters
name
Type: System.String
The section to render.
required
Type: System.Boolean
true to specify that the section is required; otherwise, false.
Change the call to:
#RenderSection("breadcrumbs", false);
If the section "required" parameter is false, it will not give an error if that section is not included by a view.

Categories

Resources