I'm just learning MVC4, so this is a very basic question.
I have a string of text that I'd like to display on multiple pages. What is the best practice to accomplish this (other than copying/pasting it into each cshtml)?
I'm just learning MVC4, so this is a very basic question.
I have a string of text that I'd like to display on multiple pages. What is the best practice to accomplish this (other than copying/pasting it into each cshtml)?
If it's going to be on every page, put it in the _layout file as Maess suggested. If you only want it in specific views, you can create a partial view and just insert that wherever you want it to display.
You could define them in code within a static class:
namespace MyNamespace
{
public static class MyConstants
{
public static string message = "Whatever I wanted to say";
}
}
and use them in cshtml:
@using MyNamespace
<h1>@ViewBag.Title @constants.message </h1>
Also have a look at the answers to VikViks question: Share constants between C# and Javascript in MVC Razor