0

Let's say I have a html file that works as a wrapper for dynamic html content, that I want to reuse something like this:

<div class="content-wrapper">
  <div class="header-styles">@Model.HeaderText</div>
    @RenderChildren()
 <div class="footer-styles">@Model.FooterText</div>
</div>

Is there a way to achieve this? The reusability for my html content wrapper is rather limited, since I have to copy/paste the html for each use case where the child content is different.

user1784297
  • 103
  • 12
  • 1
    You could use Razor's `Layout` feature. If you're using ASP.NET MVC-for-.NET Framework 4.x then your version of Razor will be somewhat different to the current Razor version used by ASP.NET Core - though `Layout` works the same: https://www.learnrazorpages.com/razor-pages/files/layout – Dai Jun 21 '23 at 12:52
  • Will that work if there are multiple wrappers on one page? Let's say I need to display that wrapper with some child content 4 times on a page, where the child content is different for each wrapper? – user1784297 Jun 21 '23 at 13:07
  • 1
    Please edit your question to include more details, such as your full HTML structure (e.g. where is your `` HTML?) and what your `@model` type is, and how you're rendering this view (are you using `View("foo.cshtml")` from within a controller-action, or is this called from ``? – Dai Jun 21 '23 at 13:24
  • I did some more googling, and found this article which seems to fit my case pretty well. https://stackoverflow.com/questions/23518499/creating-reusable-html-view-components-using-razor-in-asp-net-mvc – user1784297 Jun 21 '23 at 14:24

0 Answers0