dynamically render partial view on _layout.cshtml mvc 3 - asp.net-mvc-3

I want to display different navigation links based on the page that is loaded in my _layout.cshtml file.
I thought about checking the url and just calling Html.RenderPartial within an if block but that seems clunky.
Is there a way to control this with a controller?

If you truly need different navigation links on different pages then I think you should specify different layouts pages on these separate pages. These different layouts should then specify your _layout as their layout, making it the master layout
Ex:
_navlinks1.cshtml
#{
Layout = "_layout"
}
#RenderBody()
#section navlinks
{
#*create navlinks specific to current page*#
}
Then in your _layout page you can put #RenderSection("navlinks", false) where you want the navigation links to go.
But, if for some reason you need a distinct set of navigation links for every single page, then putting navigation links in your layout might not make sense. Might be better off having all your models inherit a base model with a list of items containing navigation link data. Then call a partial view that processes this data into the correct links in your views.

Related

ASP.NET MVC 3: Are multiple nested Views with #section not supported in #RenderSection?

Here is my setup in ASP.NET MVC 3:
Master Page --(RenderBody)-> Index View --(Html.Action)-> Partial View.
The Master Page has a #RenderSection and the Partial View has a #section block but the content isn't being included on the Master Page. I can, however, put the #section block on the Index View and that works. Are multi-nested Sections not supported in MVC 3? I really don't want to move the section block up a level but will if there is no other way.
And just FYI this particular RenderSection is at the bottom of the Master Page for including JavaScript blocks. If a partial view needs to execute some script it can include the #section block to keep everything organized.
Cheers!
You can't use #section in a partial view. You can have nested layouts, but not partials.

Resuable content in MVC view

I have a view in which I have a DIV which opens up as a JQuery dialog. This DIV has many elements including buttons and static text (No element inside this DIV is assigned any data from any model and it's not making use of any Razor syntax). Now I want to move this DIV to
another page so as the make it reusable. So that this DIV can be used in other views. What is the best way achieve this? Should I be using MVC user control?
Please Suggest
Thanks
Put the whole DIV in a partial view in your Views/Shared-folder in MVC (or another folder if you want to) . Let's assume you call this file '_DialogView.cshtml'. You can call this partial view from your normal page as a partial view, like this:
#Html.RenderPartial('_DialogView')
keep the page in a seperate htm. then use jquery.load('mypage.htm'). you can also use hash function in load as well jquery.load('main.htm#divname')
You can also use declarative helpers. See chapter "Reusing #helpers across multiple views" in ScottGu's blog here.
MyHelpers.cshtml
#helper GiveMeInput()
{
<input type="text" name"something"/>
}
Usage
#MyHelpers.GiveMeInput()

joomla add view into another view

Im using joomla MVC and I want to build a form that has different tabs which are different sections of the form with inputs in it. There are some tabs that are common to other forms that I need to include.
I would like to be able to load this common content from a separate file or view so i dont have duplicate code, plus is easier when I need to do a change to the form so I dont have to do it in all the forms. It's like displaying a view inside another view.
Is there a way to accomplish this?
A Joomla! provides the loadTemplate method to views.
So if you're currently in a tmpl file loaded for layout edit (ie. tmpl/edit.php ) you can call $this->loadTemplate('tab1'); and Joomla! will load the tmpl/edit_tab1.php file in the same view as your edit.php.
In that same view if you wanted to include say tmpl/other_tab1.php you would have to temporarily set the layout to other, eg. in one of our components during the Run template we need a tab from the Edit template, so we use:
<?php $this->setLayout('edit'); // This is ugly
echo $this->loadTemplate('plan');
$this->setLayout('run'); ?>
To load a template from another view alltogether, I think you would have to temporarily over-ride the view value, load the template then restore the view. eg.
$jinput = JFactory::getApplication()->input;
$jinput->set('view', 'other');
$this->loadTemplate('tab2');
$jinput->set('view', 'original');
NB: this last bit I haven't had time to test but it should work.
You can load a different template file for a different view manually, by simply requiring it. The following is for a view called "nameofotherview" with the layout "layoutname". If this is for an admin view use JPATH_COMPONENT_ADMINSTRATOR instead.
require(JPATH_COMPONENT_SITE . '/views/nameofotherview/tmpl/layoutname.php');
Remember that the data set up in the view class needs to be compatible with the main layout as well as the layout you're loading from elsewhere.
A side effect of doing this is that template overrides won't work. The loadTemplate function is doing a require but it checks the template paths for overrides first.
You can use vews in other views as described in Joomla documentation: Sharing layouts across views or extensions with JLayout

Razor- MVC - ASP.NET master pages - Method to somehow map multiple Razor Sections to multiple masterpage content blocks?

I have Razor working minimally with master-pages using the following technique:
http://www.hanselman.com/blog/MixingRazorViewsAndWebFormsMasterPagesWithASPNETMVC3.aspx
This works almost perfectly (Calling RenderPartial from the MasterPage on the RazorView.) The only short coming is I have 3 content blocks (controls) in my master page. Conceptually this maps nicely to a Razor Layout with 3 sections by the same names.
The only problem is I can;t figure out how to pull each section of a razor layout individually to Render it into the masterpage sections.
I tried setting the ViewBag.Layout in each masterpage section and the having 3 Razor layouts rendering the 3 razor-layout based sections [footer, header, and body]. The problem is Razor doesn't let me Render a razor view implementing multiple sections through a layout only rendering just one section. (It forces me to render all 3.)
My other attempt was to have the razor layout render the master page control markup along with the razor views. This seems elegant, but the I get an error that the masterpage control collection is read only.
Is there any way that allows me to develop Razor views to a Razor layout and have multiple layout sections map to a master pages equivalent multiple (one works perfectly) content blocks/sections?
Edit: Updated Link to the MasterPage/MVC integration article.
Layouts and Master Pagers are two different things. You can't mix and match them in the same pages. You're going to have to recreate the sections of master pages in layouts if you want to convert. Over time, you can get rid of the duplication by getting rid of the master pages.
What you could do, is extract the relevant part of both master pages into a single partial page, then inject that into both Layout and Master pages using a partial. So your content section and razor section would both just contain a single Html.Partial() that injects the common code into both.

Question on using content from other cshtml pages in razor

I've read a couple of articles on using asp.net mvc3 razor (which I am fairly new to). I have a couple of .cshmtl pages which are like shared content (e.g. header). They are basically just html pages with one or two divs etc.
To embed this in my main page, do I just use #renderPage("page address"). Do I also need a call to #renderbody? Do I need to specify the/a page in the layout property?
Thanksa
I would put the common elements in a layout (or perhaps a partial view rendered by the base layout). In fact, that's what I did in an application I am now building and it works quite nicely. The one issue is whether or not you need View Model data populated by the controller and passed to that partial view. I did, so I used a base controller and populated the common elements in the view model (all of those also inherited from a base class that had the common properties) and used sections and then in the sections renderered the partial view or not, depending on the view's need.
You can create a Partial View for each of these and call:
#Html.Partial("ViewName")
Or you can use sections, or this article on sections might help too.
As you may or may not know, ASP.NET accepts HTML tagging.
So why not include your .aspx file with the HTML include tag?
Here's how:
<!-- #include virtual="path to file/include-file.html" -->
Ex:
<!--#include virtual="header.aspx"-->
I do this all of the time when writing an ASP.NET website.
Just place it wherever you want the code, from the included page, to show up.

Resources