Solved

Including global text/code in a Razor view engine

Posted on 2014-03-05
9
387 Views
Last Modified: 2014-03-07
I need to imitate a server side include call in a Razor view engine. The view engine needs to treat the included text as 'dumb' text until it renders the entire view. (Becuase the text/code will be contained in a global area and will not 'know' the variable names contained within it.

E.G. (pseudo code - ignore specific syntax)

<@  Some Razor code.....

       var mylocalvariable = "value specific to this view";

>

<# include   globalCode.txt  -->

<@ Rest of Razor code
.....

>

***********************************

and then in globalCode.txt we will have something that uses that variable. E.G.

   if mylocalvariable = 'blah blah'  print  'blah blah' ;

****************************************


Obviously the real job is much more complicated then this but this trivial example works to get us to where we need. The problem is that Razor does not allow server side includes. And, rendering partial views want to render compiled/compilable objects (not 'dumb' text that only becomes real code after inserted into the local file.

Any idea how this can be done?
0
Comment
Question by:mcorsi62
  • 5
  • 4
9 Comments
 
LVL 96

Expert Comment

by:Bob Learned
ID: 39909091
I don't completely understand your question, but I would think that you could store the global somewhere, and then pass it to the view in the ViewData or ViewBag dictionaries.
0
 

Author Comment

by:mcorsi62
ID: 39909170
The 'global' data is cshtml code and it is the lions share of the code on the page. The 'local' data for each screen are the values used in the variables in the global data.
0
 
LVL 96

Accepted Solution

by:
Bob Learned earned 500 total points
ID: 39909240
I would normally put the "global" stuff in the _Layout.cshtml page.  The "local" data would be the individual views from the controller methods, that use the _Layout page.  Then, I would pass any needed data from the controller to the views, as needed for each view, either with the Model, ViewBag, or ViewData constructs.
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:mcorsi62
ID: 39909397
Not sure how I would do that.  Can you use layouts within layouts? We have a master _layout page that applies to the entire site. The pages I am coding use this, but they are partial views that also expose a custom 'kendo' grid. Each grid is different (hence the variables) but all the base code for the grid is the same, hence the need for a 'global' include file - however, not all pages on the site will have a grid and this is a very large project. Asking for a change to the master layout page might be a bit like trying to get a budget through congress.

Can you use layouts within layouts. In other words, the global page with the main menu. I realize even though my view doesn't directly call the layout something is set up where it automatically happens. Can i also call a special layout as well? An 'internal' layout? Or is there only on layout per view?
0
 
LVL 96

Expert Comment

by:Bob Learned
ID: 39909408
As far as I know, there is only 1 layout page.  Can you give me a small example of what your views look like? I can't quite get a good picture of your problem space.
0
 

Author Comment

by:mcorsi62
ID: 39910222
nvm - I can call a different layout from the global (main) one when I make the call to the view from the controller. So my grid layout will only be used for my grid views and it will contain the global layout plus the grid stuff.

Thanks for all the help! Look for a more difficult (related) question in a bit involving this same building of a view tied in with Kendo grids.

;-)
0
 
LVL 96

Expert Comment

by:Bob Learned
ID: 39910290
"I can call a different layout from the global (main) one when I make the call to the view from the controller."
How did you choose to do that?
0
 

Author Comment

by:mcorsi62
ID: 39912284
Something like this in the controller call to the view will override the global layout:

return View("Index", "~/Views/Shared/_GridLayout.cshtml", someViewModel);
0
 

Author Comment

by:mcorsi62
ID: 39912296
Please look at: 'Carrying data type elements to a layout/kendo Razor call'

for another, more challenging question. I can't seem to figure out how to pass a class (data type object) to the layout. It should be trivial, but it is not.
0

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Just a quick little trick I learned recently.  Now that I'm using jQuery with abandon in my asp.net applications, I have grown tired of the following syntax:      (CODE) I suppose it just offends my sense of decency to put inline VBScript on a…
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
Nobody understands Phishing better than an anti-spam company. That’s why we are providing Phishing Awareness Training to our customers. According to a report by Verizon, only 3% of targeted users report malicious emails to management. With compan…
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…

685 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question