I am new to ASP.Net MVC and have always been using Web Forms till now. I have found Replacement for ITemplate in MVC? which gives an example of creating an HTML helper named BeginBlock which wraps the title & content from:
@using (Html.BeginBlock("MY TITLE")) {
MY CONTENT GOES HERE
}
to
<div>
<div><h1>MY TITLE</h1></div>
</div>
MY CONTENT GOES HERE
I have a scenario where in Web Forms, we used to use multiple ITemplates to define containers within user controls which we then wrapped inside HTML, for example, in Web Forms, we could create a user control named Panel and have two ITemplate properties, one named Content and the other named ContentNonScrollable. We would then use the user control by using the following markup:
<MySite:Panel>
<Content>
Content 1 Goes Here
</Content>
<ContentNonScrollable>
Content 2 goes here
</ContentNonScrollable>
</MySite:Panel>
The user control would then output the following, HTML:
<div class="my-panel">
<div class="my-panel-content">
Content 1 Goes Here
</div>
<div class="my-scrollable-panel-content">
Content 2 Goes Here
</div>
</div>
Is there any way in MVC, where through HTML Helpers (or anything else), we can devise something equivalent to the above Web Forms example through markup within the .cshtml template file?
For example something like (obviously, the below doesn’t have correct syntax, just to explain what we have in mind):
@using (Html.BeginPanel() {
{
Content 1 Goes Here
}
{
Content 2 Goes Here
}
}
You can use sections for this. Sections are designed for layouts (ie master pages) but you can nest master pages to create sectioned areas.
But, it sounds like you want to do this as a control of some type. Another option might be Templated Razor Delegates
Another option is Editor/Display Templates, although this isn’t typically markup-only. You would use variables to pass the content.
Another option is just using a Partial View, and using ViewData to pass in the context sections.
There are actually a lot of different ways you can go about this, and which way you choose depends on your needs. Can you explain the specific circumstances?