1

Suppose I have many views, each displaying very similar content. Each view uses the same _Layout as defined in _ViewStart.

My _Layout might look like this:

<html>
<!-- some html -->
@ViewBag.SomeViewBagFun
<!-- more html -->
@RenderBody
<!-- more html -->
@SpecialFunction()
</html>

Helper SpecialFunction()
@<a href="/">Linky</a>
@<!-- complex HTML -->
End Helper

Suppose 90% of my pages use the default SpecialFunction() as defined in the layout, but occasionally some views want a different SpecialFunction().

One way to resolve this is to use ViewBag and send each view content that way, but SpecialFunction() contains complex HTML and I want to use razor views.

How do I achieve this?

Rowan Freeman
  • 15,724
  • 11
  • 69
  • 100
  • 1
    I believe this question is more suited for StackOverflow. It should be migrated. – rae1 Mar 12 '13 at 00:10
  • Damn. I put it here thinking it's more of a concept question rather than something overly specific. Thanks, I've reported it for migration. – Rowan Freeman Mar 12 '13 at 00:12

4 Answers4

1

The best way to do this is to use a Section and when you render it make it NOT required.

See ScottGu's blog for some details: http://weblogs.asp.net/scottgu/archive/2010/12/30/asp-net-mvc-3-layouts-and-sections-with-razor.aspx

Glenn Ferrie
  • 10,290
  • 3
  • 42
  • 73
0

You can specify the layout in the actual View, as such,

@{
    Layout = "/address/to/your/different/layout.cshtml"
}

Look at this question for further info.

Community
  • 1
  • 1
rae1
  • 6,066
  • 4
  • 27
  • 48
  • But in my example, the View still wants to use the _Layout (like all other views), just change a particular section of the layout. Is this possible? – Rowan Freeman Mar 12 '13 at 00:19
0

OP here.

It's been a while since I used ASP.NET MVC & Razor. I forgot about Sections. I feel silly ^^

Solution:

_Layout

<html>
<!-- some html -->
@ViewBag.SomeViewBagFun
<!-- more html -->
@RenderBody
<!-- more html -->
@If IsSectionDefined("SpecialFunction") Then
    @RenderSection("SpecialFunction", False)
Else
    @SpecialFunction()
End If

</html>

Helper SpecialFunction()
@<a href="/">Linky</a>
@<!-- complex HTML -->
End Helper

Another View

Section SpecialFunction
<p>Very different HTML!</p>
End Section
Rowan Freeman
  • 15,724
  • 11
  • 69
  • 100
  • 1
    I believe as you have coded it in your sample, the Section is required. Unless your section is marked as "required: false" it will be expected in EVERY view. – Glenn Ferrie Mar 12 '13 at 00:33
  • Good point. Although if I specify the Section in the _Layout, won't it always be there unless it's overridden in a View? – Rowan Freeman Mar 12 '13 at 00:35
  • Well. Yeah, but that sort of defeats the point. the idea with sections is that you render" conditionally and then you only add them to the view when you want to see/use them. Think of it like an optional parameter in a method. – Glenn Ferrie Mar 12 '13 at 00:38
  • I've updated my code to be more explicit about what's going on. Does it make more sense now? I want it so that the section has a default unless specified otherwise. – Rowan Freeman Mar 12 '13 at 00:43
0

You can add a condition to your viewbag and based on the value the layout would execute specialFfunction or someOtherFunction Scott Guthrie gives a great example of this technique in his blog

Michael Brown
  • 9,041
  • 1
  • 28
  • 37