Global variables in the Razor View Engine

Is there a way to use a feature similar to what is called Global Variables in the Spark View Engine, but for Razor.

The whole lis point should be able to define a variable in one section for the header, and then be able to set or change the value of this variable later in another section.

In Spark, you will create a variable as a section like this (incomplete code, for example, for purposes):

<html>
  <head>
    <global type='string' Title='"Site Name"'/>
    <title>${Title}</title>
  </head>
  <body>
    <div><use content="view"/></div>
  </body>
</html>

And then you can install it in another view or section or something else:

<set Title='product.Name + " - " + Title'/>

How can I do something like this in Razor or just solve a similar problem if I have the wrong approach?

+5
source share
1

ViewBag.Title :

<html>
  <head>
    <title>@ViewBag.Title - Site Name</title>
  </head>
  <body>
    <div>
        @RenderBody()
    </div>
  </body>
</html>

:

@model AppName.Models.Product
@{
    ViewBag.Title = Model.Name;
}

UPDATE:

, .

<html>
  <head>
    <title>
    @if (IsSectionDefined("Title"))
    {
        RenderSection("Title")
    }
    else 
    {
        <text>Some default title</text>
    }
    </title>
  </head>
  <body>
    <div>
        @RenderBody()
    </div>
  </body>
</html>

, :

@section Title {
    <text>some redefined title here</text>
}
+7

All Articles