Search code examples
asp.net-coreindexinglayoutdot

Layout page in dot net core not showing changes on click from Index page


I have menus on Layout page and I need to make them enabled on click of button which is there on Index view On Index button click I have called controller action which sets the flag using view bag and then I am enabling menu if the flag is set by checking condition on layout page, condition is checked correctly but still menu is not enabled, but if I am clicking any other button then menu is enabling

Layout.cshtml

 @if (@ViewBag.Enable == "true" || Context.Session.GetString("CustomerCode") == null)
                        {
                            <a href="#OCLegacy" data-toggle="collapse" aria-expanded="false" style="color:gray" class="bg-dark list-group-item list-group-item-action flex-column align-items-start disabled">
                                <div class="d-flex w-100 justify-content-start align-items-center">
                                    <span class="bi bi-diamond-fill fa-fw mr-3"></span>
                                    <span class="menu-collapsed">OC-Legacy</span>
                                    <span class="submenu-icon ml-auto"></span>
                                </div>
                            </a>
                        }

Index.cshtml

 <a asp-action="Index" style="align-self:self-end"  data-id="@customer.CustomerCode"
                          
                           class="btn btn-primary ManageSelect">

calling index action from controller


Solution

  • Here is a demo with ViewBag.Enable. pass @customer.CustomerCode to action,if the it is not null,set ViewBag.Enable to true.Else,set ViewBag.Enable to false.If ViewBag.Enable is true,male the menu enable. Layout:

    @if (@ViewBag.Enable != "true")
                {
                    <a href="#OCLegacy" data-toggle="collapse" aria-expanded="false" style="color:gray" class="bg-dark list-group-item list-group-item-action flex-column align-items-start disabled">
                        <div class="d-flex w-100 justify-content-start align-items-center">
                            <span class="bi bi-diamond-fill fa-fw mr-3"></span>
                            <span class="menu-collapsed">OC-Legacy</span>
                            <span class="submenu-icon ml-auto"></span>
                        </div>
                    </a>
                }
                else { 
                    <a href="#OCLegacy" data-toggle="collapse" aria-expanded="false" style="color:gray" class="bg-dark list-group-item list-group-item-action flex-column align-items-start">
                        <div class="d-flex w-100 justify-content-start align-items-center">
                            <span class="bi bi-diamond-fill fa-fw mr-3"></span>
                            <span class="menu-collapsed">OC-Legacy</span>
                            <span class="submenu-icon ml-auto"></span>
                        </div>
                    </a>
                }
    

    Index(asp-route-id will pass @customer.CustomerCode as a parameter id to action):

    <a asp-action="Index" style="align-self:self-end" asp-route-id="@customer.CustomerCode" data-id="@customer.CustomerCode"
                              
                               class="btn btn-primary ManageSelect">
    

    Action:

    public IActionResult Index(string id)
    {
        if (id != null)
        {
            ViewBag.Enable = "true";
        }
        else
        {
            ViewBag.Enable = "false";
        }
       
        return View();
    }
    

    result: enter image description here