Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Global Variables in Razor View Engine

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

The point of it all lis to be able to define a variable in one section for the title and then being able to set or change the value of that variable later on in another section.

In Spark you would create the variable in a section kind of like this (incomplete code for example purposes):

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

And then you could set it in a different view or section or whatever:

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

How would I go about doing something like this in Razor or just solving a similar problem if I have the wrong approach ?

like image 422
bluediapente Avatar asked Mar 04 '11 13:03

bluediapente


People also ask

How do you create a global variable in razor view?

To declare a variable in the View using Razor syntax, we need to first create a code block by using @{ and } and then we can use the same syntax we use in the C#. In the above code, notice that we have created the Code block and then start writing C# syntax to declare and assign the variables.

Is Razor pages better than MVC?

From the docs, "Razor Pages can make coding page-focused scenarios easier and more productive than using controllers and views." If your ASP.NET MVC app makes heavy use of views, you may want to consider migrating from actions and views to Razor Pages.

Why we use Razor View Engine in MVC?

Razor View Engine is a markup syntax which helps us to write HTML and server-side code in web pages using C# or VB.Net. It is server-side markup language however it is not at all a programming language.

Which namespace is used for razor view?

The namespace for Razor Engine is System. The Razor file extension is "cshtml" for the C# language. By default, Razor View Engine encodes html tags or scripts before it's being rendered to view that avoids Cross-Site Scripting attacks.


1 Answers

You could use ViewBag.Title inside the layout:

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

and then define this variable inside the view:

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

UPDATE:

Following on the comments question about default values you could use sections.

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

and then inside your view you could redefine the section if you will:

@section Title {
    <text>some redefined title here</text>
}
like image 163
Darin Dimitrov Avatar answered Oct 03 '22 12:10

Darin Dimitrov