?
Solved

Including global text/code in a Razor view engine

Posted on 2014-03-05
9
Medium Priority
?
392 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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 2000 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

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

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

A long time ago (May 2011), I have written an article showing you how to create a DLL using Visual Studio 2005 to be hosted in SQL Server 2005. That was valid at that time and it is still valid if you are still using these versions. You can still re…
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
If you’ve ever visited a web page and noticed a cool font that you really liked the look of, but couldn’t figure out which font it was so that you could use it for your own work, then this video is for you! In this Micro Tutorial, you'll learn yo…
Add bar graphs to Access queries using Unicode block characters. Graphs appear on every record in the color you want. Give life to numbers. Hopes this gives you ideas on visualizing your data in new ways ~ Create a calculated field in a query: …
Suggested Courses

777 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