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.