Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Determining the name of current view within a partial view in ASP.NET MVC

I have a partial view in my test ASP.NET MVC application. This view is responsible to display application menu buttons.

I want to change the color of button which is currently active page. Currently I have written something like:

<ul id="menu">
    <% var activeClass = (string)(ViewData["currentPage"]) == "Home" ? "activeMenuButton" : ""; %>    
    <li><%= Html.ActionLink ("Home", "Index", "Home", new { @class = activeClass })%></li>

    <% activeClass = (string)(ViewData["currentPage"]) == "About" ? "activeMenuButton" : ""; %>    
    <li><%= Html.ActionLink ("About", "About", "Home", new { @class = activeClass })%></li>
</ul>

And set the view data in controller actions:

//in home action
ViewData["currentPage"] = "Home";

//in About action
ViewData["currentPage"] = "About";

This works but I have to modify every controller action. Is there any better method of automatically detecting the view and somehow change the partial view code to change the color accordingly.

like image 947
Hemant Avatar asked Jul 17 '09 08:07

Hemant


People also ask

Can we call partial view inside partial view in MVC?

To call a partial view from another partial view, we follow the same approach as we follow when we call partial view from the content page. Let's take an example. This is the code of 1st partial view. Notice that in above partial view, we are calling another partial view named "_View2" that is created below.

How do you call a partial view from another view?

To create a partial view, right click on the Shared folder -> click Add -> click View.. to open the Add View popup, as shown below. You can create a partial view in any View folder. However, it is recommended to create all your partial views in the Shared folder so that they can be used in multiple views.

How can we call a partial view in view of ASP NET core?

In ASP.NET Core MVC, a controller's ViewResult is capable of returning either a view or a partial view. In Razor Pages, a PageModel can return a partial view represented as a PartialViewResult object. Referencing and rendering partial views is described in the Reference a partial view section.

How do you tell if a view is a partial view?

Partials often have an underscore prefix in their name. Other than that, nothing really separates them from other views.


1 Answers

I think you dont need ViewData["currentPage"]. Use ViewContext.RouteData.Values instead:

  • SO - Getting the name of the controller and action method in the view in ASP.Net MVC

Try this:

<ul id="menu">
    <% var activeClass = (ViewContext.RouteData.Values["Controller"] == "Home"
        && ViewContext.RouteData.Values["Action"] == "Home") ? "activeMenuButton" : ""; %>

    <li><%= Html.ActionLink ("Home", "Index", "Home", new { @class = activeClass })%></li>

    <% activeClass = (ViewContext.RouteData.Values["Controller"] == "Home"
        && ViewContext.RouteData.Values["Action"] == "About") ? "activeMenuButton" : ""; %>

    <li><%= Html.ActionLink ("About", "About", "Home", new { @class = activeClass })%></li>
</ul>
like image 143
eu-ge-ne Avatar answered Sep 30 '22 15:09

eu-ge-ne