AliAzra
AliAzra

Reputation: 919

How to get the current Action name inside the View

In my _Layout page, I have got a search form and each controller has an index view. When the user clicks the search button, it searches in the current index view.

I want to show the search field if the user is index view if they go to other views, I wanted to hide it.

In my _Layout

 <form asp-action="Index" method="get" class="navbar-form form-inline navbar-right">
        <input class="form-control mr-sm-2" id="search" name="search" type="search" placeholder="Search" aria-label="Search">
        <button class="btn btn-outline-success my-2 my-sm-0"  id="BtnSearch" name="BtnSarch" type="submit">Search</button>
    </form>

I am using JQuery at the moment but it is quite difficult to put every single view

    $("#search").hide();
    $("#BtnSearch").hide();

Basically, in my _Layout page, I wanted to show or hide Search form if the user is in the index view. how can i get current view name in _Layout view, please?

Upvotes: 6

Views: 1992

Answers (4)

Gareth Balfour
Gareth Balfour

Reputation: 176

This sounds like it is the ideal candidate for mvc tag helpers.

You will need to create a class which inherits from TagHelpers and override the process method.

[HtmlTargetElement(“website-search”)]
Public class Search : TagHelper
{
    Public WebsiteContext Info { get; set; }

    Public override void Process(TagHelperContext context, TagHelperOutput output)
    {
        Output.TagName = “section”;
        Output.Content.SetHtmlContent(“ HTML for your search form “);
        Output.TagMode = TagMode.StartTagAndEndTag;
    }
}

In order to get the controller and action you will need to add a property to the tag helper:

[HtmlAttributeNotBound]
[ViewContext]
Public ViewContext ViewContext { get; set; }

Now that you have the view context in place, you can look to do something like the following:

If(ViewContext.RouteData.Values[“action”]) != “Index”)
{
    Output.SuppressOutput();
}

You can then reference this by putting website-helper in your view.

Please see the following link for an intro on tag helpers https://learn.microsoft.com/en-us/aspnet/core/mvc/views/tag-helpers/intro?view=aspnetcore-2.2

There is the following stack overflow question detailing how to get the controller and action executed against : Knowing what route the user is on inside TagHelper in .NET Core

Hope this helps

Upvotes: 2

Christoph Herold
Christoph Herold

Reputation: 1809

If you want to show the search form only in specific views, I would not base this on the view name. In the future, you might also need it in other views. So, why not simply add a flag to show the search form to your ViewBag. It will mean, setting this flag in every "Index" action, but you will be more flexible with where to show it.

Controller:

public ActionResult Index()
{
    this.ViewBag.ShowSearch = true;
    // … your normal code
    return this.View();
}

_Layout.cshtml

@if (this.ViewBag.ShowSearch == true) // explicitly check for true, so not having set the flag in the ViewBag will not pose a problem, i.e. null != true.
{
    <form action="">@* … *@</form>
}

Upvotes: 1

Nan Yu
Nan Yu

Reputation: 27528

Basically, in my _Layout page, I wanted to show or hide Search form if the user is in the index view.

Try with below codes :

@if ("Index".Equals(ViewContext.RouteData.Values["Action"].ToString()))
{
    <form asp-action="Index" method="get" class="navbar-form form-inline navbar-right">
        <input class="form-control mr-sm-2" id="search" name="search" type="search" placeholder="Search" aria-label="Search">
        <button class="btn btn-outline-success my-2 my-sm-0" id="BtnSearch" name="BtnSarch" type="submit">Search</button>
    </form>
}

Upvotes: 3

Toan Bach Ngoc
Toan Bach Ngoc

Reputation: 11

You can add a hidden input to layouts file and assign to it an id. Then you can get action and controller name from anywhere:

<input type="hidden" value="@this.ViewContext.RouteData.Values["action"].ToString()" />
<input type="hidden" value="@this.ViewContext.RouteData.Values["controller"].ToString()" />

So if you don't use them in JS, you can declare a variable and show your form when action is Index.

Hope to help.

Upvotes: 1

Related Questions