425

I am developing a web application in asp.net mvc 3. I am very new to it. In a view using razor, I'd like to declare some local variables and use it across the entire page. How can this be done?

It seems rather trivial to be able to do the following action:

@bool isUserConnected = string.IsNullOrEmpty(Model.CreatorFullName);
@if (isUserConnected)
{ // meaning that the viewing user has not been saved
    <div>
        <div> click to join us </div>
        <a id="login" href="javascript:void(0);" style="display: inline; ">join</a>
    </div>
}

But this doesn't work. Is this possible?

Owen Pauling
  • 11,349
  • 20
  • 53
  • 64
vondip
  • 13,809
  • 27
  • 100
  • 156

7 Answers7

617

I think you were pretty close, try this:

@{bool isUserConnected = string.IsNullOrEmpty(Model.CreatorFullName);}
@if (isUserConnected)
{ // meaning that the viewing user has not been saved so continue
    <div>
        <div> click to join us </div>
        <a id="login" href="javascript:void(0);" style="display: inline; ">join here</a>
    </div>
}
Community
  • 1
  • 1
Tomas Jansson
  • 22,767
  • 13
  • 83
  • 137
  • How do you do this in VB.NET? – Stefan Paul Noack Jan 18 '12 at 13:51
  • 8
    oh, i found it out by myself: `@Code .. End Code` instead of `@{ .. }` – Stefan Paul Noack Jan 18 '12 at 13:55
  • 1
    @Abhijeet.Nagre, in the question he writes: `@bool isUserConnected = string.IsNullOrEmpty(Model.CreatorFullName);` but variable definition must be inside a "code block". I can't give a better answer to why then that, it's just how razor works. – Tomas Jansson Apr 08 '15 at 08:04
  • 3
    @AbhijeetNagre - Razor is **usually** quite good at understanding where code starts and ends, but it's not perfect. Sometimes we just have to give it a little hint regarding what should be treated as Razor/C#, and what shouldn't. If you ever get a Razor error, adding `{ }` tags is usually the first step – Jon Story Oct 08 '15 at 10:16
  • @StefanPaulNoack I think you can use parentheses `(` `)` instead of brackets `{` `}` for VB. – mekb Jul 11 '19 at 08:31
  • Inside the brackets `{ }` of a code block (@for, @if, ...), the variable can be initialized without the `@`. – Flimtix May 25 '22 at 12:30
62

I think the variable should be in the same block:

@{
    bool isUserConnected = string.IsNullOrEmpty(Model.CreatorFullName);
    if (isUserConnected)
    { 
        // meaning that the viewing user has not been saved
        <div>
            <div> click to join us </div>
            <a id="login" href="javascript:void(0);" style="display: inline; ">join</a>
        </div>
    }
}
Flimtix
  • 356
  • 1
  • 4
  • 17
Khasha
  • 1,559
  • 1
  • 12
  • 19
  • That seems to be the case, in MVC3 at least. – Matthew Walton Apr 22 '13 at 15:10
  • 1
    Excellent! Any idea how you would then use the variable `isUserConnected` again further down the page? – SharpC Oct 21 '15 at 10:57
  • @SharpC Once you declare a variable like that it's available in the rest of that .cshtml file. Later in the file you could do something like `@if (isUserConnected) { /* stuff if connected */ }` or `
    Connected? @isUserConnected
    ` (this works better with strings). It's not available outside that file though (e.g. you'll have to declare it separately in partials).
    – Dan Mangiarelli Oct 22 '15 at 14:11
18

You can also use:

@if(string.IsNullOrEmpty(Model.CreatorFullName))
{
...your code...
}

No need for a variable in the code

DBS
  • 9,110
  • 4
  • 35
  • 53
marcel
  • 313
  • 4
  • 10
18

Not a direct answer to OP's problem, but it may help you too. You can declare a local variable next to some html inside a scope without trouble.

@foreach (var item in Model.Stuff)
{
    var file = item.MoreStuff.FirstOrDefault();

    <li><a href="@item.Source">@file.Name</a></li>
}
aloisdg
  • 22,270
  • 6
  • 85
  • 105
14

If you're looking for a int variable, one that increments as the code loops, you can use something like this:

@{
  int counter = 1;

  foreach (var item in Model.Stuff) {
    ... some code ...
    counter = counter + 1;
  }
} 
Brian
  • 3,653
  • 1
  • 22
  • 33
4

If you want a variable to be accessible across the entire page, it works well to define it at the top of the file. (You can use either an implicit or explicit type.)

@{
    // implicit type
    var something1 = "something";

    // explicit type
    string something2 = "something";
}

<div>@something1</div> @*display first variable*@
<div>@something2</div> @*display second variable*@
mfluehr
  • 2,832
  • 2
  • 23
  • 31
d384
  • 49
  • 1
1

you can put everything in a block and easily write any code that you wish in that block just exactly the below code :

@{
        bool isUserConnected = string.IsNullOrEmpty(Model.CreatorFullName);
        if (isUserConnected)
        { // meaning that the viewing user has not been saved
            <div>
                <div> click to join us </div>
                <a id="login" href="javascript:void(0);" style="display: inline; ">join</a>
            </div>
        }
    }

it helps you to have at first a cleaner code and also you can prevent your page from loading many times different blocks of codes

sajadre
  • 1,141
  • 2
  • 15
  • 30