1

I use a sidebar on my website with stats from my database and statics data like links and other texts.

In my _Layout.cshtml, I use Html.RenderAction("SidebarPV", "Home"); to call the sidebar.

The sidebar is a Partial-View using a ViewModel for the stats. SidebarPV is generated in my HomeController like that :

public ActionResult SidebarPV() {
    SidebarViewModel viewmodel = new SidebarViewModel();
    DateTime now = DateTime.Now;
    viewmodel.stat_data1 = db.Table1.Where(e => e.DateDeb <= now && e.DateFin >= now).Count();
    viewmodel.stat_data2 = db.Table2.Where(c => c.DateDeb <= now && c.DateFin >= now).Count();
    return PartialView("SidebarPV", viewmodel);
}

It works like a charm but I don't need stats on all views, only on /Home/Index

So I want to 'comment' the stats generation when the ser is not on the index of the website.

Thanks for advices.

EDIT (solution, thanks to krillgar) :

I wrote in my _Layout

@{
    var isHome = ViewContext.RouteData.Values["controller"].ToString().ToUpper() == "HOME";
    var isIndex = ViewContext.RouteData.Values["action"].ToString().ToUpper() == "INDEX";
    if (isHome && isIndex) {
        Html.RenderAction("SidebarPV", "Home");
    }
    else {
        Html.RenderAction("SidebarNoStatPV", "Home");
    }
}

I know I need to create two partial-views but one is static so I will not edit it for a long time :). Tested, It works.

2
  • 1
    Put the partial on your home index and not your layout.
    – lucuma
    Jul 28, 2014 at 15:26
  • That can be impossible if it's going to be buried somewhere in the HTML markup that would become inaccessible inside the Home/Index view.
    – krillgar
    Jul 28, 2014 at 15:29

2 Answers 2

5

There's also another solution. As for me, i hate things like this

ViewContext.RouteData.Values["controller"].ToString().ToUpper() == "HOME"

It has to work with magic strings, which is not even a const, just runtime string. It may provide some issues in advance. And what if you will need it on some another page?

I'd recommend you to use nested layouts. You can create _Layout with section

UPDATE:

_Layout.cshtml:

<...>
@Html.RenderSection("sidebar", false)
<...>

And then on your home page you may just use _SidebarLayout instead of _Layout. And whenever you will need sidebar on any page, you can do just the same.

So your home page will look like

@{
    Layout = "~/Views/_Layout.cshtml";
}

@section sidebar {
    @Html.RenderAction("SidebarPV", "Home")
}

And all other pages will look like

@{
    Layout = "~/Views/_Layout.cshtml";
}

@section sidebar {
    @Html.RenderAction("SidebarNoStatPV", "Home")
}

If you don't want to repeat yourself with this "SidebarNoStatPV" you can use nested layouts:

_NoStatLayout.cshtml

@{
    Layout = "~/Views/_Layout.cshtml";
}
@section sidebar {
    @Html.RenderAction("SidebarNoStatPV", "Home")
}

and use it as the layout for any page but Home. If you will need to extend sidebar with additional info for different pages you can just put @Html.RenderSection("sidebar", false) inside sidebar section in _NoStatLayout.cshtml.

Why do i consider it as a better option? It fits the SRP, for only Home page should be responsible for it's own unique data.

3
  • Thanks for proposition. I need sidebar on all pages, just some data in the sidebar don't need to appear on all pages.
    – Alex L
    Jul 29, 2014 at 7:53
  • That's a great idea, and not something I'd considered doing most of the time. Thanks for the tip.
    – krillgar
    Jul 29, 2014 at 12:06
  • We already use multiple _Layout in our project and I prefer to not create an other. I think your solution is more 'clean' too. Our website is an Intranet and will not on the Internet so the first solution will not be a problem. I keep your for my others project :)
    – Alex L
    Jul 29, 2014 at 12:17
4

If you want to keep the code to call the action in the _Layout page, then you just need to get information of what action is being called when generating the page. Add the following to the top of your _Layout:

var isHome = ViewContext.RouteData.Values["controller"].ToString().ToUpper() == "HOME";
var isIndex = ViewContext.RouteData.Values["action"].ToString().ToUpper() == "INDEX";

Then wrap your call to generate the partial view wherever you need it in _Layout with the following:

if (isHome && isIndex) {
    Html.RenderAction("SidebarPV", "Home");
}
2
  • Cannot do that cause I want to 'comment' a part of my Partial-View code itself called in the layout. I tried to put this code in the Partial-View but don't work. Maybe, with this code, I can call two differents Partial-Views in the layout... I'll try that way.
    – Alex L
    Jul 29, 2014 at 6:49
  • 1
    First post edited, the solution was here. Thank you.
    – Alex L
    Jul 29, 2014 at 7:03

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.