HiveBrain v1.2.0
Get Started
← Back to all entries
snippetcsharpCritical

How to declare a local variable in Razor?

Submitted by: @import:stackoverflow-api··
0
Viewed 0 times
howdeclarerazorvariablelocal

Problem

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
    
         click to join us 
        join
    
}


But this doesn't work. Is this possible?

Solution

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
    
         click to join us 
        join here
    
}

Code Snippets

@{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>
}

Context

Stack Overflow Q#6601715, score: 638

Revisions (0)

No revisions yet.