Asp.net mvc RenderAction RouteData Controller and Action values

I am trying to create a RenderAction method on the main page that will dynamically generate a sidebar based on the current controller and action. When a RenderAction is called in a view, it is populated by the controller and the action of this particular RenderAction method. For example, if I am on the Home controller and the Index action, I expect the Home and Index in the GenerateSideBar method. Instead, I get a Home controller and a RenderSideBar action. Thank you for your help!

My main page:

   <div class="side-bucket">
    <%
        string Controller = ViewContext.RouteData.Values["Controller"].ToString();
        string Action = ViewContext.RouteData.Values["Action"].ToString();
    %>
    <% Html.RenderAction("RenderSideBar", "Home", new { controller = Controller, action = Action }); %>

Controller action:

public ActionResult GenerateSideBar(string controller, string action)
        {
            switch (controller.Trim().ToLower())
            {
                case "member" :
                    return View(@"sidebar\MemberSidebar");
                default:
                    break;
            }

            return null; 
        }
+3
source share
1 answer

, , ​​ , . :

public ActionResult GenerateSideBar(string currentController, string currentAction)
{
    ...
}

:

<% Html.RenderAction("RenderSideBar", "Home", 
    new { currentController = ViewContext.RouteData.Values["controller"], 
          currentAction = ViewContext.RouteData.Values["action"] }
); %>

. , , [ChildActionOnly].

+5

Source: https://habr.com/ru/post/1769987/


All Articles