asp.net mvc : multiple layout - asp.net-mvc

I have a main layout(layout1) of my website as a start page and now I want to add another page(layout) that gets some information from user and then after saving the information in database render the main layout.
right now in start page :
Layout = "~/Views/Shared/_Layout.cshtml";
and in layout :
<div id="main" class="container">
#RenderBody()
#RenderSection("~/Views/AskGroup/_Layout1.cshtml");}
</div>
the error : Section not defined: "~/Views/AskGroup/_Layout1.cshtml". how should I add thid layout1 to the layout page? do I need to add a controller as well ?

Since it's basic HTML content, use RenderPartial instead:
#Html.RenderPartial("_Layout1.cshtml");

Related

How to get to partial views in ASP.NET Core 3.0

I have a Core 3.0 project using Areas and in an Area view I am trying to reference a partial view from the main project Views>Shared folder.
Project>Views>Shared>_layout_partial.cshtml << desired view partial.
Project>Areas>MyArea>Views>Index.cshtml << this view is trying to access the partial described above.
<div>
<div class="container-fluid">
<partial name= "~/Views/Shared/_layout_partial1.cshtml"/>
<partial name="~Views/Shared/_layout_partial2.cshtml"/>
<partial name="~/Views/Shared/_layout_partial3.cshtml"/>
</div><!-- container -->
</div>
This works fine when the main view is in Project>Views>Home>Index.cshtml but the partial view is not recognized all when the main view is in an area and no warnings or errors.
Is it possible to access this partial view from an area or do I have to do serious could duplication.
For partial, it is using Microsoft.AspNetCore.Mvc.TagHelpers, here are two options for you:
Change to use
#await Html.PartialAsync("/Views/Shared/_layout_partial1.cshtml")
If you prefer partial tag, you need to reference the Microsoft.AspNetCore.Mvc.TagHelpers by copying _ViewImports.cshtml from Views Folder to your Areas Folder

Using a single Sitecore view rendering multiple times on the same page with different data

I am new to developing for Sitecore using MVC. I have a page layout that looks like this:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
#Html.Sitecore().Placeholder("head-meta-content")
</head>
<body>
<div class="willow-page">
#Html.Sitecore().Placeholder("body-content")
</div>
</body>
</html>
And I have the following renderings.
Global alert:
#using Sitecore.Mvc;
#model Sitecore.Mvc.Presentation.RenderingModel
<section class="global-alert" role="alert">
<button class="global-
alert__close">#Html.Sitecore().Field("button-text", Model.Item)
</button>
<h1 class="global-
alert__heading">#Html.Sitecore().Field("heading-text", Model.Item)</h1>
<div class="global-alert__content">
#Html.Sitecore().Placeholder("global-alert-content")
</div>
</section>
Styling Context:
#using Sitecore.Mvc;
#model Sitecore.Mvc.Presentation.RenderingModel
<div class="styling-context">
#Html.Sitecore().Field("Content", Model.Item)
<!-- The Content field is should be a Rich Text Field -->
</div>
What I am trying to accomplish/understand is this. When creating a page using the layout and the 2 renderings, I'd like to add the Global Alert to the body-content place holder on the layout and then add a Styling Context to the global-alert-content place holder. Also I'd like to add another Styling Context to the body-content place holder as well. Like this:
Then when viewing the page content tab in Sitecore, I'd like to see a section with a rich text field for the Global Alert Styling Context & the Body Styling context. I can't seem to figure out how to setup my data to accomplish this.
I've tried to setup the following data templates.
Global Alert Template - Builder
Global Alert Template - Content
Notice the _stylingContext in the Base Template area above.
Styling Context - Builder
Blank Page - Page Template
Note the inherited templates above.
The global alert template is pulling in a styling context template, & the page template is pulling a separate styling context template, I would think there'd be 2 on the page. There isn't though.
This is the normal behavior of Sitecore. The reason is because the same template is being referenced in the Base Template. So, when rendering the item, Sitecore will see that the template has already been rendered and will not render it again.
If template A has Base Template X and X has Base Template Y, it is not possible to have template A to have Y as base template also.
A => X
X => Y
which means A => Y
To allow for the 2 Rich Text to appear, you can do the following:
Create a Base Styling Context
On your template Styling Context, add your Content Field of type Rich Text in the Builder tab and in the Content tab, add the Base Styling template.
On your Global Alert template, in the Content tab, add the Styling Context template.
On your blank Page template, add the Global Alert only and not the Styling Context.
Now if you see on the Standard Values of the Blank Page template, you will see 2 rich text fields with the same field name within the same section:

How to add site navigation (ascx) to MVC _Layout.cshtml page

I am migrating a WebForm asp.net project to MVC. I copied and pasted the masterpage content to the _Layout.cshtml page. Now I got the side panel for the site Navigation. In the Webform, as shown in the code below, the put the site navigation to the far left. I tried both the enf and ~/Views/SiteNavigation.ascx, but the site navigation control is not appearing when I load up the page. Just a blank left panel. Any help is appreciated.
<div class="horizontalTabBarContent" id="mainTabContent">
<enf:SiteNavigation ID="ISRNavigation" />
#*~/Views/SiteNavigation.ascx*#
<div id="main">
<asp:ContentPlaceHolder ID="ISRContent" >
</asp:ContentPlaceHolder>
</div>
</div>
</asp:Panel>
I gess you're looking for Html.RenderPartial helper.
In Razor syntax it will be:
#{ Html.RenderPartial("~/Views/SiteNavigation.ascx"); }
First of all,
A view in MVC is basic HTML code in which you can add razor syntax. The "Razor code" will be parsed server side and will generate html output to bind models, generate valid url etc.
Here is an example of what your _Layout.cshtml view might look like:
<div class="horizontalTabBarContent" id="mainTabContent">
<div ID="ISRNavigation" />
#Html.Partial("~/Views/YourPartialNavigationView") //Your navigation bar will be rendered here
<div id="main">
#RenderBody() //Body html code will be rendered here
</div>
</div>
MVC Projet don't support the asp:htmltags syntax
in MVC instead of web forms user control, you should use partial views and by using of Helper functions like RenderPartial and Partial you could render it througth your view !!
and also instead of using PlaceHolder you should use Section !
you can Google for both topics : partialView , Section

RefineryCMS trying to tweak layout per page

I'm new to RefineryCMS and still pretty new to rails as well. What i'm trying to do, is use the same header and footer in my layout file and then in the dynamic section that gets pulled from the CMS database, use different markup per page.
Example template:
<header>
...
</header>
# want to insert "home" page or "about" page here depending on url.
<footer>
...
</footer>
Example of what i want home page to have inserted into template:
<div>
<%= raw #page.content_for(:body) %>
</div>
Example of what i want about page to have inserted into template:
<div>
<div>
<div>
<%= raw #page.content_for(:body) %>
</div>
</div>
</div>
How can i change the markup per page without having to add the markup in the wysiwyg editor in the CMS?
Which version are you using?
If you're using 2.0.x, then there are two options inside config/initializers/refinery/pages.rb — the first one should be config.view_template_whitelist, and the second one should be config.use_view_templates.
For config.view_template_whitelist, you can specify an array of symbols that match to views inside your app/views/refinery/pages/ folder (i.e. if you have about_us.html.erb, you would whitelist [:about_us], just as you might if you were going to say render :about_us inside your controller.
config.use_view_templates simply needs to be set to true to enable the dropdown in the backend that will let you choose a template from your whitelist.
If you're curious, there also exists config.layout_template_whitelist and config.use_layout_template options too that do the same, but with layouts.
If you wish to change the header rather than the whole layout then you can override the _header partial:
cp /var/lib/gems/1.8/gems/refinerycms-core-2.0.6/app/views/refinery/_header.html.erb app/views/refinery/_header.html.erb
..and then edit app/views/refinery/_header.html.erb and restart the application ( if required).
This allowed me to place a header image above the nav menu without creating a custom layout.

Serving static content to my action using MVC's convention approach

I'm looking at outsourcing some in-page help on a large web application I am creating and would like to make it really easy for this content to added to our pages when we're ready for it.
So I was thinking I could create a system where I can add text files to the same folder where an action expects it's view to be and read out the content of that file the content in that file can be passed back to the view to display. Either that or create a helper that would do the same.
Example
Controllers
HomeController.cs
Views
Home
Index.aspx
Index.txt
Index.aspx would then have access to the content in Index.txt.
How would I start going about creating this system. Are there any built in classes in .NET MVC that I could take advantage of?
A similar question was asked yesterday: Including static html file from ~/Content into ASP.NET MVC view.
Basically you can read the text from the file and include it inside your view by using File.ReadAllText so you would have something like this inside your index.aspx file
<%= File.ReadAllText(Server.MapPath("~/Views/Home/index.txt")) %>
I'd create a parallel hierarchy in the Content folder and put the files there, probably as HTML. Then you can simply load them via AJAX in the view using the parallel hierarchy convention.
Content
Help
Home
index-help.html
about-help.html
Foo
index-help.html
bar-help.html
Then in your views
<div class="help">
<noscript>
<a href='#Url.Content( "~/content/help/home/index-help.html" )'>Click for Help</a>
</noscript>
</div>
<script type="text/javascript">
$(function() {
$('.help').load( '#Url.Content( "~/content/help/home/index-help.html" )' );
});
</script>
You may also be able to extract the controller/action from RouteData in the view if your routes are consistent and move this to your _Layout.cshtml file with the path being provided by route data.
#{
var controller = ViewContext.RouteData["controller"] as string;
var action = ViewContext.RouteData["action"] as string;
var url = Url.Content( string.Format( "~/content/help/{0}/{1}-help.html", controller, action ) );
<div class="help">
<noscript>
<a href="#url>Click for Help</a>
</noscript>
</div>
<script type="text/javascript">
$(function() {
$('.help').load( "#url" );
});
</script>
}
One possible solution would be to store them as xml file instead, that are serialized from the model the view is expecting. You could then create an Action Filter populate the model being returned with the data from the XML file. I hope that helps.

Resources