Rob White
Rob White

Reputation: 1002

MVC Layout Sections

Is there a way I can set a piece of text in a view to appear in multiple places in my layout?

I've tried using rendersection but you aren't allowed to use two of the same name and I don't want to create two with different names as I then duplicate the text in the view.

I don't want to set the viewbag in the controller either.

Upvotes: 0

Views: 33

Answers (1)

RickL
RickL

Reputation: 3393

Can't you just set the ViewBag value in your particular Razor View:

@{
    ViewBag.SampleText = "This is some sample text";
}

and sprinkle around @ViewBag.SampleText in your layout?

There is a better alternative to using multiple ViewBags for layout properties (with different values) that involves more coding. and setting values in the Controller, but from what you're describing this should hopefully work out.

Upvotes: 1

Related Questions