3 Replies - 7602 Views - Last Post: 18 September 2012 - 01:19 PM Rate Topic: -----

#1 rbflowers  Icon User is offline

  • New D.I.C Head

Reputation: 2
  • View blog
  • Posts: 48
  • Joined: 10-February 12

Global Razor Variables

Posted 17 September 2012 - 12:00 PM

Is there a way to create razor variables where they can be accessed by all of my .cshtml pages?

I have a form where users can update their information (update.cshtml). When the user submits, I want his information to be updated on his info page (userX.cshtml).

Here's an example of how I get the information:
if(post){
string name= Request["Name"]; 
<h1>@name</h1>
}
else {
 <form method="post" action="">
Name:  <input type="text" name="Name" style="height:16px; font-size:14px;"/>
</form>
}



Is the only way to access the variable name accross other pages is to throw it into a database?

This post has been edited by rbflowers: 17 September 2012 - 12:01 PM


Is This A Good Question/Topic? 0
  • +

Replies To: Global Razor Variables

#2 RudiVisser  Icon User is offline

  • .. does not guess solutions
  • member icon

Reputation: 1004
  • View blog
  • Posts: 3,562
  • Joined: 05-June 09

Re: Global Razor Variables

Posted 17 September 2012 - 12:15 PM

You could always extend all of your controllers from a base controller which will define a variable in the ViewBag, or put it in _ViewStart.cshtml.
Was This Post Helpful? 0
  • +
  • -

#3 rbflowers  Icon User is offline

  • New D.I.C Head

Reputation: 2
  • View blog
  • Posts: 48
  • Joined: 10-February 12

Re: Global Razor Variables

Posted 17 September 2012 - 12:28 PM

View PostRudiVisser, on 17 September 2012 - 12:15 PM, said:

You could always extend all of your controllers from a base controller which will define a variable in the ViewBag, or put it in _ViewStart.cshtml.


How would I do the first (defining variable in ViewBag? The only controller I have is HomeController.cs, which I only use to make sure the tab links work.

For the second (_Viewstart.cshtml), how would I send the variable there? I tried creating a dummy variable in this page named test, but when I called it from my index.cshtml, it couldn't find the variable:

Description: An error occurred during the compilation of a resource required to service this request. Please review the following specific error details and modify your source code appropriately.

Compiler Error Message: CS0103: The name 'test' does not exist in the current context

Source Error:


Line 13:
Line 14:
Line 15: <h1>@test</h1>
Was This Post Helpful? 0
  • +
  • -

#4 h4nnib4l  Icon User is offline

  • The Noid
  • member icon

Reputation: 1182
  • View blog
  • Posts: 1,677
  • Joined: 24-August 11

Re: Global Razor Variables

Posted 18 September 2012 - 01:19 PM

Is there any reason that you couldn't use session variables? I know that they are different in MVC, but I have limited MVC experience so I don't know exactly how.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1