Collapse sections in cshtml view - asp.net-mvc

Is there a way to collapse a razor code block or section in a cshtml file?
As you can in the image above, the div is collapsable but not the section or the code block.
Any ideas?

You could try this Visual Studio extension:
https://visualstudiogallery.msdn.microsoft.com/bc07ec7e-abfa-425f-bb65-2411a260b926
One of the reviews claims that it allows you to collapse #helper and #section code blocks in MVC4 views but it doesn't seem to work for everyone.

You can add a section in CSHTML files like this:
<!-- #region name -->
wrapped content...
<!-- #endregion -->
This works at least since VS2015:
Note: You can even wrap the HTML comments in CSHTML comments (#* ... *#)

Related

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

Can I choose not to render a certain section in the layout page? [duplicate]

This question already has answers here:
Is it possible to make Razor sections optional?
(5 answers)
Closed 8 years ago.
I have an ASP.NET MVC layout page that is used by all the views in my application. Each of these views defines a "PanelRight" section, that is rendered in the layout page. However, now I would like to replace that section with some other markup. I thought it'd be as easy as commenting out the RenderSection instruction and adding my new markup, but I keep getting an exception ("The following sections have been defined but have not been rendered for the layout page").
I understand what the exception means; I only want to know how to get rid of it. Id est, can I NOT render a certain section that is defined in the views? Because I'd much rather not go through all of them and removing the section.
EDIT
I'll try to be more clear.
Suppose this is my layout page:
<html>
<head> <!-- HEAD STUFF --> </head>
<body>
#RenderSection("RightPanel", false)
#RenderBody()
</body>
</html>
and this is one of my many views:
#section RightPanel {
<div>This is the markup of the section</div>
}
<div>This is the body of my view</div>
Now I decide that, rather than having a RightPanel section that can be customized by each view, I want to have a fixed content instead. So I "remove" the section and replace it with the markup I want to use - straight into the layout (actually it will be in another partial, but not in a section, that's the point).
Of course, I don't want to go through all of my many views to delete the #section RightPanel { }. I just want the layout page to ignore the section. To make a comparison, it's like I defined a class with a DoFancyStuff() method, and then in the client code I never call that method. Not a problem there, so I see no reason why it would be here.
To recap: the view defines a section, the layout doesn't intend to render it. This results in an error. Is there a way to do this?
No, you cannot have a section in your view that your layout does not implement. This will result in a runtime error, and there's no way around that. The reason everyone is telling you how to implement optional sections is because the layout must implement any section any view might want to use, but, by making it optional, you can allow some views to use some sections while other views use other sections.
So, if I want to "comment out" a RenderSection, if just for a test, do I really have to go through all views and remove that section definition?
Technically, yes. Although you could simply wrap the section in regular HTML comments <!-- -->, so while it's rendered, it's rendered as an HTML comment instead of actual DOM elements. Also, with layout inheritance, you can sort of short circuit the views' sections by defining an empty section. For example:
_Layout.cshtml
#RenderSection("Foo")
_SubLayout.cshtml
#{ Layout = "~/Views/Shared/_Layout.cshtml"; }
#section Foo {}
SomeView.cshtml
#{ Layout = "~/Views/Shared/_SubLayout.cshtml"; }
#section Foo
{
<!-- stuff here -->
}
The section Foo would be empty because the sub-layout implements Foo but doesn't also call #RenderSection("Foo") itself, so there's no opportunity for a view using it as a layout to alter that section. That won't really help you now much, but it's good to know for future reference.
I suppose you could create a new layout with a different name and move the stuff from your current layout there while making your current layout inherit from the new layout. That would allow you to implement this without having to update all your layout references in your views. However, it will likely be easier to just to use HTML comments to comment out your section in your layout for testing purposes.
UPDATE
Just one more thing. Now that I'm thinking about it, the layout inheritance approach I suggested as one potential method, may actually not work. It occurred to me that you may still get an error because the sub-layout doesn't call #RenderSection. It all depends on whether the initial call in the main layout is enough to satisfy. HTML comments are definitely the safest and easiest approach.
UPDATE BY OP
Accepted answer:
you could simply wrap the section in regular HTML comments <!-- -->, so while it's rendered, it's rendered as an HTML comment instead of actual DOM elements.

Umbraco Dashboards: custom markup within tabs and specific content page

In my tab from home page I want to render a partial view returned by an action controller, with custom css. The home page has its own doctype. Using Umbraco v7.
How can I achieve this? I read http://our.umbraco.org/wiki/reference/files-and-folders/dashboardconfig but doesn't specify this.
You can only load a usercontrol (ASCX) in the dashboards.
But that shouldn't stop you from what you want to do.
Put your Html and css in the ASCX and
put your controller code in the .cs file.
update the /config/dashboard.config
And you are set.
What you could do is wrap your partial view in a macro and call the macro inside the ascx (<umbraco:Macro alias="theMacroAlias" runat="server" />)
Update: you can't set a dashboard for a specific content page. The only way to archive something here is to create your down propertyType

MVC Razor helper does not exist in current context

I have created a custom razor helper in my MVC4 Web application that I need to be usable in all of my views.
In all of my view pages, I can't seem to use my custom helper. VS2012 doesn't just see it.
How Can I resolve this please ?
EDIT: It actually works when I run the page, it's just VS that doesn't see it.
Here is my helper which is located in Helpers.cshtml within my AppCode folder.
#helper TextBox(string title, string id, string placeholder, bool required){
<ul>
<li>
<label for="#id">#title</label>
</li>
<li>
<input type="text" name="this" class="#if (#required) {<text>required</text>}" minlength="2" id="#id" placeholder="#placeholder" />
</li>
</ul>
}
Restart Visual Studio
Clean and rebuild alone was not enough but the steps that worked for me were:
Clean solution
Restart Visual Studio (2012)
Rebuild solution
After those steps, the Visual Studio Intellisense picked it up again.
Try to build/rebuild the project (if your helper is in the App_Code folder).
Then VS will recognize the helper.
If it is razor helper(using #helper syntax), you should define it in view placed within \App_Code
We can accomplish this by saving our #helper methods within
.cshtml/.vbhtml files that are placed within a \App_Code directory
that you create at the root of a project. For example, below I
created a “ScottGu.cshtml” file within the \App_Code folder, and
defined two separate helper methods within the file (you can have any
number of helper methods within each file):
And if it is more traditional html helper, you should reference it, by adding record to namespaces element of <system.web.webPages.razor> defined in ~\Views\Web.Config. If you want to use it only in singe view, you could add #using directive on top of view.
In any view you could call your custom Razor helper like this:
#Helpers.TextBox("some title", "someid", "default value", false)
This assumes that your helper is defined inside ~/App_Code/Helpers.cshtml.

In ASP.NET MVC, what is the advantage of using #RenderSection (versus say #RenderPage)?

I would like to be enlightened if I'm doing something wrong. I bet I am.
If I have the following code in my view page:
#{
ViewBag.Title = "About Us";
}
<h2>About</h2>
<p>
sample content.
</p>
#section header
{
<div id="header">
Chapter 3a: Creating a Consistent Look
</div>
}
...I should also have the ff in my layout page to render the section (if available) to prevent an exception at run-time:
#if (IsSectionDefined("header"))
{
#RenderSection("header")
}
However, if I don't have the last 4 lines above that checks for the section before rendering the section (say, I commented them all), the compiler will not check that I have a section defined in my view page, and allow me to build and run the application. During run-time, it is only then that I will get this error when I run the page:
The following sections have been defined but have not been rendered
for the layout page "~/Views/Shared/_Layout2.cshtml": "header".
My questions then are the ff:
How can we prevent this from happening? Is there any setting that forces the compiler to check for the missing #RenderSection code?
Doesn't the usage of the #section feature make a system less maintainable (assuming question #1 has no positive answer) since we need to manually search for the presence of the #section keyword throughout the entire application?
In this case, what is the advantage of using #RenderSection then as opposed to #RenderPage?
Can we also make the #section conditional?
I've never thought of this as an issue. The concept is the same of placeholders in aspx syntax, so if you have 2 placeholders in your homepage, you're supposed to have 2 contents in each page/view using that masterpage.
There's a blog post of Phil Haack on the argument, it doesn't address your concerns directly but it's surely something interesting to consider.
http://haacked.com/archive/2011/03/05/defining-default-content-for-a-razor-layout-section.aspx
i know its old but if someone passed by this.
#RenderSection("header", required: false) by this you can either have a
#section header { } in your view page or not.
And now you can also remove the check condition from your layout.
also by this its conditional!
if you want it to be required you can :
#RenderSection("header", required: true) but by this if your view doesn't have a #section header it will throw an error.

Resources