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?
You can also use:
No need for a variable in the code
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.
to declare a var to be accessed across the page....top of the page usually works out. implicit or explicitly your choice.
you can put everything in a block and after easily write any code that you wish in that block just exactly the below code :
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
If you're looking for a int variable, one that increments as the code loops, you can use something like this:
I think the variable should be in the same block: