James Wilson
James Wilson

Reputation: 5150

Reusing a bit of code

<Unique code>

<CodeBlock1>

<UniqueCode>

<CodeBlock1>

This is the basic layout of my .cshtml page.

This is a c# MVC 4 Razor web project.

The above code is in my _Layout.cshtml page.

What is a way I can remove both <CodeBlock1> from that page and move the code to a single place and still show it in two places in my _Layout.cshtml page.

I assume I add a _Navigation.cshtml (its nav code that is duplicated)

But how would I then show that code on my _Layout.cshtml page?

Upvotes: 0

Views: 97

Answers (1)

Brandon
Brandon

Reputation: 69993

Create your partial view called _Navigation.cshtml and then use Html.Partial("_Navigation") to include it in your _Layout.cshtml page.

Something like

<Unique Code>

@Html.Partial("_Navigation")

<Unique Code>

@Html.Partial("_Navigation")

Upvotes: 2

Related Questions