Extend element beyond _Layout container MVC ASP.NET (preferred method?) - asp.net-mvc

I'm doing a little experimenting with MVC / Bootstrap / ASP.NET...
Specifically I'm trying to understand a best practice to allow an element to extend past its container based on a given logic, in this case based on the loaded view. (I've read through the threads on showing elements based on a Role membership as well).
I'm trying to replicating the layout shown here: URL To Sample. It has a bootstrap carousel and I like the placement extending the entire width but only showing on the Index (Home) page. Implementing this outside of the classic MVC structure is trivial.
With MVC we have the shared _Layout.cshtml and the container where views are loaded with ...
<div class="container body-content">
#RenderBody()
</div>
It seems the carousel needs either
be placed on the _Layout page and hidden if the loaded view is not the Index page or
be placed on the Index page and allowed to exceed the width of the container
I've tried what I've been able to find in threads but haven't been able to replicate this layout cleanly. Can someone please give a bit of direction?

You could stretch it with css, or a cleaner solution could be to put it in its own (optional) section:
in _Layout:
#RenderSection("carousel", required: false)
<div class="container body-content">
#RenderBody()
</div>
in your front page/home (index) file:
#section carousel {
// code for carousel
}
As commented by #Alex, if your home page is significantly different from the other pages, you can use a separate layout file. To do this, just set the "Layout" global string like this;
in your front page/home (index) file:
#{
Layout = "~/Views/Shared/_FrontPageLayout.cshtml"; // to use a separate file
Layout = null; // to not use a layout file
}

Related

MVC Areas Overlayed Shared Layouts

I'm using Visual Studio to make an MVC App with sub-apps in Areas. In the root _Layout.cshtml I have a navbar-fixed-top that will have navigation to the different areas. I want this perpetuated on every single view. Then I modified the css to create a navbar-fixed-left where I will have navigation within just the particular area and that needs to be unique per area, but used on all views within that area.
So my question is, is there a way to use a shared layout within a shared layout so I don't redundantly copy and eventually screw up the top nav?
You can use the RenderSection('leftNavigation', false) in the html of your _layout page.
from the view that has left navigation you can fill the section
#section leftNavigation
{
<ul>
Some line
</ul>
}
you can use the #section in the _viewStart.cshtml of the area is you have navigation that is valid for the complete area.

Inherited layout in ASP.NET MVC

I have default layout _Layout.cshtml for the most pages. However for some group of pages I would like to have slightly modified default layout. I know I could just copy that file a modified it a bit, but it would mean to duplicate the code and maintain two layout with 99% of same code.
I would like to inherit the layout from the default like this:
LayoutInherited.cshtml:
Layout = "~/Views/Shared/_Layout.cshtml";
ViewBag.BodyContentClassSpecial = "";
#section header{
<style>
#body-content {
width: 90%;
margin: 0 auto;
}
</style>
}
It is possible to do something like this?
Yes, layout inheritance is possible. Essentially, you're just creating a layout that itself utilizes a layout, since layouts are just views, there's no issues with that.
You pretty much do it exactly as you described:
_SubLayout.cshtml
#{
Layout = "~/Views/Shared/_Layout.cshtml";
}
#RenderBody()
A few things to keep in mind:
The content of the sub-layout will be placed where you have #RenderBody in your base layout, just as the content of a view would be. Your sub-layout still needs its own #RenderBody to determine where the content of the view that utilizes it should be placed.
Any sections defined as required in your base layout must be implemented in your sub-layout or Razor will raise an exception, just as if your view did not implement the section. For example:
_Layout.cshtml
#RenderSection("Foo", required: true)
_SubLayout.cshtml
#section Foo
{
<p>Foo</p>
}
If your view needs to be able to implement a section (required or not), the sub-layout must define it. For example, in the code above, any view using _SubLayout.cshtml would not be able to define a Foo section, because it would no longer exist. An exception would be raised if you tried. In order to allow that view to define that section you would have to do something like the following:
_SubLayout.cshtml
#section Foo
{
#RenderSection("Foo", required: true)
}
This defines the section for the purpose of the base layout and then allows the section to be defined by any view that uses this sub layout.
There's actually a post on my blog that goes into all this in much greater detail if you need it: http://cpratt.co/how-to-change-the-default-asp-net-mvc-themet/

Rendering Backbone view into multiple elements with different configurations

I have a html page with 3 jquery-mobile page widgets. Each page widget has the same header with a slightly different configuration for sub links. I want to create the Header as a BackboneView and render it for each page widget. Each page widget has it's own view as well. The page template looks like this:
<body>
<div id="news" data-role="page">
<header class="header"></header>
</div>
<div id="updates" data-role="page">
<header class="header"></header>
</div>
<div id="stuff" data-role="page">
<header class="header"></header>
</div>
</body>
The view for the first page, in this case being news view, gets rendered first. This page creates an instance of the header view. The problem arises when working with the header.
1) How do I target the header specifically for the first page, "#news header" while keeping it flexible enough for me to call it again with a different set of parameters when I am ready to load the updates page?
2) When I'm ready to load the updates page, it will have a different set of configurations that need to target "#updates header". Therefore I don't want to hardcode tagName on the view.
3) How do I pass the first instantiated header to the rest of the views? Or am I thinking about this all wrong and should I be extending a base Header view as a template for each of my pages which come baked in with a set of configs?
The only thing that is different between each header is a class name it will attach to an element in the rendered view and a few anchor links it makes for the page. For this, I am trying to reuse the view as much as possible to have a light footprint.
Note: Not sure if it matters but I'm using requirejs. Maybe there is a suggested way to create a View and always have it reused when necessary since requirejs handles modules as singletons?
Thanks!
Use an overview like appView.js. It will be in charge to render all the subviews.
In appView (you'd need to require your subviews in this super module, HeaderView):
initialize: function() {
this.$news = $('#news');
this.$updates = $('#updates');
this.$stuff = $('#stuff');
},
render:function() {
var newsView = new HeaderView(param1,param2);
this.$news.append(newsView.render().el);
// do the same for your other subviews
}
In your subviews (lets say NewsView)
initialize:function(param1,param2) {
this.viewclass= param1;
//set the other things you need here
},
render: function() {
this.$el.html("my content here..model/template/anything"));
this.$el.attr('class',this.vewClass);
}
Remember that the point is to have an overview creating new SubViews and calling the render(). I hope it helps.
Edit: If you are waiting for some data to load a specific subview, use a promise. If you are waiting for a collection to finish to fetch use something like that
collection.fetch(function() {
success:function(res,model) {
new subView(model);
}
}

The following sections have been defined but have not been rendered for the layout page "~/Views/Shared/_Layout.cshtml"

I know there are a few questions that have been answered but I didn't find something specific to my case.
I'm using the mobile capabilities of MVC4. So I created a _layout.mobile.cshtml and the corresponding views.
The error above happens when I go in with a mobile device. As you can see, it is trying to display the regular _layout.cshtml instead of the _layout.mobile.cshtml. So I'm assuming it is also trying to display the view (say Index.mobile.cshtm) which doesn't have the section in question. Basically it is mixing the regular layout with the mobile views.
This doesn't happen all the time. If I recycle the pool it works again for a while and then all of the sudden it goes back to having the error and it will continue until I recycle the pool again.
Has anyone seen this problem before that can shed some light?
Thanks
John
In the _ViewStart.cshtml available under the Views folder, change the Layout value to your custom layout. I think this may help.. (Make sure that you are returning View instead of partial view)
for example
#{
Layout = "~/Views/Shared/_layout.mobile.cshtml";
}
In case if you want to change the layout for a specific page you can explicitly define it at the top of the page as a page directive.
in the index.cshtml there is a section being called defined in the original layout file "_LayoutHome.cshtml" that is not defined in the new bootstrap layout.
specifically: #RenderSection("featured", required: false)
So the solution is to either add this section to the new layout (look for it in the original layout and paste it) or simply delete it from the index.cshtml.
I had also face the same problem I removed
#section featured {
From View
Another way to do this is to use a conditional block in your _ViewStart.cshtml page. For example, you may have two layouts depending on the device regular user. Using pseudo-code for the reading of the device/browser type bit, it would look something like this:
#{
if(userIsMobile)
{
Layout = "~/Views/Shared/_MobileLayout.cshtml";
}
else
{
Layout = "~/Views/Shared/_Layout.cshtml";
}
}
I have used this to display or hide sections or menu items as needed for different classes of user; it should work as well for device-specific layouts.
Joey Morgan

Common layout with different design parts for each controller in ASP MVC 4

I have some base layout with site structure. In this default layout I define header tag, body structure and footer:
<html>
<head>...</head>
<body>
<div id="sidebar">...</div>
<div id="entry">#RenderSection("Entry", true)</div>
<div id="footer">...</div>
</body>
Each action in each controller defines in their view own entry section.
<!-- in View/Index.cshtml -->
#section Entry {
Hello from Index action.
}
<!-- in View/Uploads.cshtml -->
#section Entry {
Hello from Uploads action.
}
<!-- in View/Users.cshtml -->
#section Entry {
Hello from Users action.
}
But I also want define different sidebars for each controller. If I put #RenderSection("SideBar", true) in main layout I must repeat sidebar code for each controllers action. I also can't define sidebar design code in main layout because I want use different sibebars for each controller (but I want use same sidebar for each action in controller).
How can I solve this problem without repeating sidebar design code in each view?
I find solution https://stackoverflow.com/a/5573970 but it will be require duplication base site structure for each controller.
Thanks for answers and sorry for my bad english :(.
Put #RenderSection("SideBar", false)
Then you can define the section for certain controller layouts only.
You might want to define separate layouts for each controller and each view in this controller will use controller layout (where you can put your sidebar) instead of default layout. Controller layouts will use default layout.

Resources