locked
beginner level question about menus RRS feed

  • Question

  • User1457412228 posted

    I have a second menu that I would like to display when any one of five pages are loaded.  

    I currently display it as shown below.  Isn't there a way to display second menu that doesn't require changes in all five places if I were to change it?

    (I have two more second menus with different content to do and would like to do them correctly.

    It would be something that would know to load right after the _LayoutPage.

    <div>
     <partial name="partials/_RowTwoMenu" />
    </div>

    Sunday, June 2, 2019 6:47 PM

Answers

  • User-1764593085 posted

    Hi talldaniel,

    As above has said that you could use ViewData in current view and  layout view, something like:

    _Layout.cshtml:

    @if (ViewData["ShowThis"] as bool? == true)
    {
    <!-- HTML here -->
         <partial name="partials/_RowTwoMenu" />

    }

    EachoneOfFiveView.cshtml:

    @{
        ViewData["ShowThis"] = true;
    }

    You could show same content to five pages or different content for different pages by changing the ViewData value. 

    Another way it to use section.This would give you the ability to optionally replace the HTML with something else.

    _Layout.cshtml:

    @if (!IsSectionDefined("Foo"))
    {
        <!-- HTML here -->
    }
    else
    {
        @RenderSection("Foo", required: false)
    }

    EachoneOfFiveView.cshtml:

    @section Foo {
      <partial name="partials/_RowTwoMenu" />
    }

    Refer to https://stackoverflow.com/questions/47993010/asp-net-core-pass-data-from-content-page-to-layout

    Best Regards,

    Xing

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, June 4, 2019 6:41 AM

All replies

  • User-1764593085 posted

    Hi talldaniel,

    Could you be more specific about your question? You could also share us some code you have tired or expected result/effect you would like to receive.

    Best Regards,

    Xing

    Monday, June 3, 2019 5:11 AM
  • User1034446946 posted

    use the viewbag on the controller to send an indicator of which menus it needs to show, put all menus in partial views, then use and if statement against the view bag to display the partial view or not.

    Monday, June 3, 2019 3:45 PM
  • User-1764593085 posted

    Hi talldaniel,

    As above has said that you could use ViewData in current view and  layout view, something like:

    _Layout.cshtml:

    @if (ViewData["ShowThis"] as bool? == true)
    {
    <!-- HTML here -->
         <partial name="partials/_RowTwoMenu" />

    }

    EachoneOfFiveView.cshtml:

    @{
        ViewData["ShowThis"] = true;
    }

    You could show same content to five pages or different content for different pages by changing the ViewData value. 

    Another way it to use section.This would give you the ability to optionally replace the HTML with something else.

    _Layout.cshtml:

    @if (!IsSectionDefined("Foo"))
    {
        <!-- HTML here -->
    }
    else
    {
        @RenderSection("Foo", required: false)
    }

    EachoneOfFiveView.cshtml:

    @section Foo {
      <partial name="partials/_RowTwoMenu" />
    }

    Refer to https://stackoverflow.com/questions/47993010/asp-net-core-pass-data-from-content-page-to-layout

    Best Regards,

    Xing

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, June 4, 2019 6:41 AM
  • User1457412228 posted

    About ten years ago I used a technique (I didn't do much web inbetween, maybe a couple years) where all files in a particular folder would get an additional menu.  I don't remember how to do it.

    Thanks

    Wednesday, June 19, 2019 3:25 AM
  • User-2054057000 posted

    You can also use View Component to display the second menu. Once the View Component is created then It can be called like this on your View:

    @await Component.InvokeAsync("YourViewComponent")
    Wednesday, June 19, 2019 3:30 AM
  • User1457412228 posted

    Thank you for the help Xing,

    Apparently the way I thought I remembered it could be done was incorrect.

    Wednesday, June 19, 2019 1:15 PM