Skip to content

Instantly share code, notes, and snippets.

@celston
Last active August 21, 2017 22:24
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save celston/dcc16a8768a12c3bd0ee4f4e15cadd02 to your computer and use it in GitHub Desktop.
Save celston/dcc16a8768a12c3bd0ee4f4e15cadd02 to your computer and use it in GitHub Desktop.
MVC Razor Dynamic Sections
<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width" />
<title>_Layout</title>
</head>
<body>
<div>
<ul>
@foreach (var tab in ((List<Tuple<string, string>>)ViewBag.Tabs))
{
<li>@tab.Item2</li>
}
</ul>
@foreach (var tab in ((List<Tuple<string, string>>)ViewBag.Tabs))
{
<div>@RenderSection("Tab" + tab.Item1)</div>
}
@RenderBody()
</div>
</body>
</html>
@{
ViewBag.Title = "Index";
Layout = "~/Views/Shared/_Layout.cshtml";
ViewBag.Tabs = new List<Tuple<string, string>>()
{
new Tuple<string, string>("Red", "Red"),
new Tuple<string, string>("Yellow", "Yellow"),
new Tuple<string, string>("Blue", "Blue"),
new Tuple<string, string>("Green", "Green")
};
}
@section TabRed {
<p>The quick brown fox...</p>
}
@section TabYellow {
<p>...jumped over...</p>
}
@section TabBlue {
<p>...the lazy dog.</p>
}
@section TabGreen {
<p>The End</p>
}
@{
ViewBag.Title = "Index";
Layout = "~/Views/Shared/_Layout.cshtml";
ViewBag.Tabs = new List<Tuple<string, string>>()
{
new Tuple<string, string>("1", "One"),
new Tuple<string, string>("2", "Two"),
new Tuple<string, string>("3", "Three")
};
}
@section Tab1 {
<p>The quick brown fox...</p>
}
@section Tab2 {
<p>...jumped over...</p>
}
@section Tab3 {
<p>...the lazy dog.</p>
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment