I have one controller called Manager and I want to have additional menu for all views. I need only add something to _Layout.cshtml before and after '#RenderBody()'. How Can I do this using controller/views?
Example:
This is how all of Views from ManagerController now looks like:
<div id="content">
<h2>Projects Manager</h2>
<p>
Only for admin.
</p>
</div>
#{Html.RenderAction("ManagerMenu", "Manager");}
I want to change it to (and keep the same result):
<h2>Projects Manager</h2>
<p>
Only for admin.
</p>
How can I do this?
My idea is:
#if ("ProjectManager.Controllers.ManagerController" == ViewContext.Controller.ToString())
{
<div id="content">
#RenderBody()
</div>
{Html.RenderAction("ManagerMenu", "Manager");}
}
else
{
#RenderBody()
}
But I don't know how it should be done properly...
Why not create an optional section, and only implement that section in the Views of the Manager controller?
#RenderSection("ManagerMenu", false);
Then do this in the ManagerController views:
#section ManagerMenu {
#Html.RenderAction("ManagerMenu", "Manager")
}
Related
I have a controller that returns one of two partial view depending on condition.
Controller
public ActionResult ReviewCart(DepartmentProductViewModel model)
{
if(somecondition)
{
return PartialView("_View1", model);
}
return PartialView("_View2", model);
}
In my View I have Two tabs one for _View1 and other for _View2 with div tags. Like
Tab 1
<div id="shopping1">
#Html.Partial("_View1", Model)
</div>
Tab 2
<div id="shopping2">
#Html.Partial("_View2", Model)
</div>
In my Ajax response I would like check the if controller is returning _View1 then I would like to
$('#shopping1').html(data);
and if controller is returning _View2 then I would like to
$('#shopping2').html(data);
Any idea how to achieve this in Ajax success call.
Thanks
If I understood correctly, this example might help.
with help of jquery-ui tabs
<script>
$( function() {
$("#tabs").tabs({
beforeLoad: function( event, ui ) {
ui.jqXHR.fail(function() {
ui.panel.html(
"Couldn't load this tab. We'll try to fix this as soon as possible. " +
"If this wouldn't be a demo." );
});
}
});
} );
</script>
</head>
<body>
<div id="tabs">
<ul>
<li>Partial Content 1</li>
<li>Partial Content 2</li>
</ul>
<div id="tabs-1">
</div>
</div>
You may need pass a parameter to distinct two partial views from each other to render and change the action method itself accordingly.
E.g Url.Action('ReviewCart','Controller',new { view=1 })
This is my first post so please be gentle with me.
I have set my master layout to call "main"
<div class="row">
{{$main}}
</div>
and I have a view called home.blade
<div class="col-md-9">
<div class="content">
{{$content}}
</div>
</div>
<div class="col-md-3">
<aside class="sidebar">
{{$sidebar}}
</aside>
</div>
and in my controller I have:-
$this->layout->main = View::make('home')->nest('content', 'property.viewmany', compact('properties'));
This works for the content variable, but at this point I'm stumped at how to get the sidebar variable to work. Is there a way to nest the two variables into the home.blade from the controller?
I've attempt various, albeit, shots in the dark, but I always get either content not defined or sidebar not defined.
You can put a first variable in the first view like this:
$this->layout->main = View::make('home', compact('sidebar'))->nest('content', 'property.viewmany', compact('properties'));
So I have this master/detail setup with a WebGrid. All is well until I try and use RenderPage to display the detail when a record is clicked:
<div class="innerbox">
#{
if(gdEligibility.HasSelection){
#RenderPage("~/Views/Eligibility/EligibilityPolicyDetailView.cshtml",
new { Customer = gdEligibility.SelectedRow })
}
}
</div>
Everything works, if I put a break point I can step through the detail view's cshtml file no problem, no errors. But nothing is ever rendered between the outer div's. Ever. Why doesn't RenderPage return anything? I even tried adding .ToHtmlString() on the end of the line but still nothing.
The detail cshtml:
#{ foreach(TravelInsurance.Models.Policy p in Page.Customer.Policies){
<fieldset>
<legend>Policy</legend>
<div class="display-label">Policy Number</div>
<div class="display-field">
#Html.DisplayFor(model => p.PolicyNumber)
</div>
<div class="display-label">Premium</div>
<div class="display-field">
#Html.DisplayFor(model => p.Premium)
</div>
</fieldset>
}}
This question suggests that RenderPage will use the parent model...
Maybe try using
#{Html.RenderPartial("EligibilityPolicyDetailView", new { Customer = gdEligibility.SelectedRow });}
or
#Html.Partial("EligibilityPolicyDetailView" ,new { Customer = gdEligibility.SelectedRow })
Pl. try again by removing the attribute "ajaxUpdateContainerId" in the grid definition and it should work.
I have a MVC3 application, based on default layout from VS 2010, which I changed to looklike in image below
The submenu area is defined in _layout.cshtml as
<div id="sidebar">
<h3>Entities</h3>
<p></p>
<ul>
#Html.Partial("_EntitiesMenu")
</ul>
</div>
<section id="main">
#RenderBody()
</section>
and the _EntitiesMenu contains entries as below
<li>#Html.ActionLink("Addresses", "Index", "Address")</li>
<li>#Html.ActionLink("Applications", "Index", "Application")</li>
I have a single MapRoute defined as
routes.MapRoute("Default", // Route name
"{controller}/{action}/{id}", // URL with parameters
{ controller = "Home", action = "Index", id = UrlParameter.Optional });
All my entity controllers started from menu are defined standard in Controllers and views in Views.
What I need is to change the app to use a layout as below
When users click Entities app should navigate to myapp/entities/ or myapp/entities/index and it should open a view in main work area that will look like below
Then when users click on right submenus, the url should look like myapp/entities/entity1/index, myapp/entities/entity1/edit/1, etc (exactly like now but "under" entities page.
I defined the Entities controller like
public class EntitiesController : Controller
{
public ActionResult Index()
{ return View();}
}
And it's view looks like
<div id="workarea">
// here should became new Body region, to load all views called from the other controllers
// something like #RenderBody(), but this don't works
</div>
<div id="sidebar">
<h3>Entities</h3>
<ul>
#Html.Partial("_EntitiesMenu")
</ul>
</div>
I don't want to have to make changes to entities controllers or views (or minimal changes if absolutely necessary, because there are lot of them). Can I somehow assign that area as main Body, while under scope of entities? And if user click on top Home / About, it will "unload" the EntitiesView from _layout.cshtml?
Not sure if my question is very clear, but I hope someone will understand what I'm after.
Thank you
Are you talking about #RenderSection http://blogs.msdn.com/b/marcinon/archive/2010/12/08/optional-razor-sections-with-default-content.aspx
I managed (sort of) to accomplish something close to what I need using following approach:
Changed the _layout as below
<section id="main">
<div>
#RenderBody()
</div>
<div>
#RenderSection("EntityCRUD", false)
</div>
</section>
Created the view for Entities as:
#Html.Partial("_PanelEntitiesMenu")
Defined _PanelEntitiesMenu as
<div id="sidebar">
<h3>Entities</h3>
<p></p>
<ul>
#Html.Partial("_EntitiesMenu")
</ul>
</div>
Enclosing the entities views (Index, Edit / Delete / Details / Create) in
#section EntityCRUD
{
#Html.Partial("_PanelEntitiesMenu")
//... original view code
}
Changed all involved views to have the view "body" included in section, and at the beginning of the section I load the panel menu as partial view
#section EntityCRUD
{
#Html.Partial("_PanelEntitiesMenu")
....
}
Not exactly what I wanted, but that's the best I found so far.
I have a partial view which consists of a div with a benner that I change once a week.
This banner is embedded within 10 pages that have the same layout.
Give a page like Index.cshtml or AboutUs.cshtml, both being partial views and having the laypout _Layout.cshtml my question is:
Can I inser code in Index.cshtml or AboutUs.cshtml that renders Banner.cshtml?
something like:
inside Index.cshtml or AboutUs.cshtml I have #Html.Renderbanner("banner.cshtml");
Yes,
#{ Html.RenderPartial("_Banner"); }
In your shared view folder, add your partial view and call it _Banner.cshtml
Sure, you could always:
#Html.Partial("Banner")
from any view or partial view. This will render the Banner.cshtml partial at the place where you called this helper.
Use #Html.RenderPartial("YourViewName");
First, just to clarify: Are Index and AboutUs really partial views in your scenario? That may
change my advice, but normally I see three options:
Razor Helper, create Banner.cshtml in App_Code with the following code:
#helper Show(){
<img src="mybanner.png"/>
}
Then call in Index.cshtml:
#Banner.Show()
Html Helper Extension, a bit overkill here probably (see tutorial on web)
Partial View: Create "_MyBanner.cshtml" and use the following in Index.cshtml:
#Html.Partial("_MyBanner");
(Number 3 is the one that might be tricky if Index and AboutUs are also partial views, but 1 the one I would choose.)
Note: There is a difference between Html.Partial and Html.RenderPartial. The latter writes directly to the output stream and will require you to call it in parentheses. In MVC AFAIK it is best practice to use Html.Partial.
You can create a RenderSection in layout.cshtml
#if (IsSectionDefined("Sidebar"))
{
<div id="page">
<!-- end #content -->
<div id="content">
#RenderBody()
</div>
<div id="sidebar">
#RenderSection("Sidebar", required: false)
</div>
<div style="clear: both;">
</div>
</div>
<!-- end #page -->
<!-- end #sidebar -->
}
else
{
<div id="page">
<!-- end #content -->
<div id="content2">
#RenderBody()
</div>
<div style="clear: both;">
</div>
</div>
<!-- end #page -->
<!-- end #sidebar -->
}
if you need see the secction in about.cshtml
use
#section Sidebar{
#Html.Partial("_yourbanner")
}
if you dont need the banner do not include it