using if statement for root layout

This topic was automatically closed 365 days after the last reply. New replies are no longer allowed.
12 anos atrás
Hi ,

I have got a partial view for breadcrumb in the Root.cshtml, what I want is to use if statement to call that partial depending on the page called , for instance i want to call breadcrumb for all the pages except the topics one which are contact us , links etc .. below is the code:

<div class="master-wrapper-page">
        <div class="master-wrapper-content">
        
            @Html.Partial("Header")
            @Html.Partial("HeaderMenu")
            @* want to use if here if("otherpages"){@Html.Partial("Breadcrumb")}*@
            @Html.Partial("Breadcrumb")
            @Html.Action("WidgetsByZone", "Widget", new { widgetZone = Nop.Core.Domain.Cms.WidgetZone.BeforeContent })
            @RenderBody()
            @Html.Action("WidgetsByZone", "Widget", new { widgetZone = Nop.Core.Domain.Cms.WidgetZone.AfterContent })
            <div class="clear">
            </div>
        </div>
        @Html.Action("Footer", "Common")
    </div>
Can anyone assist me with this issue.
12 anos atrás
Hi, do you use Visual Studio ? That would make Razor a lot easier for you.

Anyway, This is how you could determine one or another.

@{

    ICategoryService categoryService = EngineContext.Current.Resolve<ICategoryService>();

    string currentController = Url.RequestContext.RouteData.Values["controller"].ToString().ToLower();
    string currentAction = Url.RequestContext.RouteData.Values["action"].ToString().ToLower();

    bool isHome = (currentController.Equals("home") && currentAction.Equals("index"));
    bool isContactUs = (currentController.Equals("common") && currentAction.Equals("ContactUs"));
    bool isInformatie = (currentController.Equals("topic"));
    
    int currentCategoryId = 0;
    if (Url.RequestContext.RouteData.Values["controller"].ToString().Equals("catalog", StringComparison.InvariantCultureIgnoreCase) &&
        Url.RequestContext.RouteData.Values["action"].ToString().Equals("category", StringComparison.InvariantCultureIgnoreCase))
    {
        currentCategoryId = Convert.ToInt32(Url.RequestContext.RouteData.Values["categoryId"].ToString());
        Category currentCategory = categoryService.GetCategoryById(currentCategoryId);
        isblablabla= (currentCategoryId == 24 || currentCategory.ParentCategoryId == 24);
        isblablabla2 = (currentCategoryId == 25 || currentCategory.ParentCategoryId == 25);
        isblablabla3 = (currentCategoryId == 23 || currentCategory.ParentCategoryId == 23);        
    }
}


@if (blablabla1 || blablabla2 etc etc etc)
{
dothis or dothat
}
12 anos atrás
I have got like 50-60 categories including subcategories, wouldnt that will be too much for And operators , or if i do something like save all the present categories in an array and then pass that array in to for each loop .
12 anos atrás
How does this question relate to your earlier question about the if statement & breadcrumb
12 anos atrás
creative3k wrote:
I have got like 50-60 categories including subcategories, wouldnt that will be too much for And operators , or if i do something like save all the present categories in an array and then pass that array in to for each loop .


Creative3k I would avoid using an if statement to determine the page for the scenario outlined in your original question. The code is going to get confusing and probably very irritating. What you should do instead is make separate layout pages.

If you don't want to duplicate the work in across two layout pages then you can sublayout the page. Something like:

_RootTopLevel - (New name for the code in _Root, define a section called creativebreadcrumbs and only display it if it is provided)

_Root - Use the section creativebreadcrumbs
_RootForTopics - Do not use the section creativebreadcrubs.
12 anos atrás
skyler.severns wrote:
I have got like 50-60 categories including subcategories, wouldnt that will be too much for And operators , or if i do something like save all the present categories in an array and then pass that array in to for each loop .

Creative3k I would avoid using an if statement to determine the page for the scenario outlined in your original question. The code is going to get confusing and probably very irritating. What you should do instead is make separate layout pages.

If you don't want to duplicate the work in across two layout pages then you can sublayout the page. Something like:

_RootTopLevel - (New name for the code in _Root, define a section called creativebreadcrumbs and only display it if it is provided)

_Root - Use the section creativebreadcrumbs
_RootForTopics - Do not use the section creativebreadcrubs.



Here are some examples:

The following code should go into your _RootTopLevel where you want the breadcrumbs to appear:


    @if (IsSectionDefined("creativebreadcrumbs"))
    {
        @RenderSection("creativebreadcrumbs")
    }



The next snippet of code should go in the layout all the pages except topics use.


@section creativebreadcrumbs {
@Html.Partial("Breadcrumb")
}
This topic was automatically closed 365 days after the last reply. New replies are no longer allowed.