Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Including global text/code in a Razor view engine

Posted on 2014-03-05
9
Medium Priority
?
395 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
What is SQL Server and how does it work?

The purpose of this paper is to provide you background on SQL Server. It’s your self-study guide for learning fundamentals. It includes both the history of SQL and its technical basics. Concepts and definitions will form the solid foundation of your future DBA expertise.

 

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

Efficient way to get backups off site to Azure

This user guide provides instructions on how to deploy and configure both a StoneFly Scale Out NAS Enterprise Cloud Drive virtual machine and Veeam Cloud Connect in the Microsoft Azure Cloud.

Question has a verified solution.

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

Introduction This article shows how to use the open source plupload control to upload multiple images. The images are resized on the client side before uploading and the upload is done in chunks. Background I had to provide a way for user…
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…
Visualize your data even better in Access queries. Given a date and a value, this lesson shows how to compare that value with the previous value, calculate the difference, and display a circle if the value is the same, an up triangle if it increased…
In this video, Percona Solutions Engineer Barrett Chambers discusses some of the basic syntax differences between MySQL and MongoDB. To learn more check out our webinar on MongoDB administration for MySQL DBA: https://www.percona.com/resources/we…

604 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