1

Есть Helpers который отвечает за добавления класса Active в меню.

public static class HtmlHelpers
{
    public static string IsSelected(this IHtmlHelper html, string controller = null, string action = null, string status = null, string cssClass = null)
    {
        if (String.IsNullOrEmpty(cssClass))
            cssClass = "active";

        string currentAction = (string)html.ViewContext.RouteData.Values["action"];
        string currentController = (string)html.ViewContext.RouteData.Values["controller"];
        string currentStatus = (string)html.ViewContext.RouteData.Values["status"];


        if (String.IsNullOrEmpty(controller))
            controller = currentController;

        if (String.IsNullOrEmpty(action))
            action = currentAction;

        if (String.IsNullOrEmpty(status))
            status = currentStatus;

        return controller == currentController && action == currentAction && status == currentStatus ?
            cssClass : String.Empty;
    }

    public static string PageClass(this IHtmlHelper htmlHelper)
    {
        string currentAction = (string)htmlHelper.ViewContext.RouteData.Values["action"];
        return currentAction;
    }

}

пока участвуют стандартные параметры URL ("controller}/{action}/{id?}") все прекрасно работает. Но как прочитать переменные в URL.

Есть https://localhost:/Contractors?status=false

Как получить данные STATUS

P.S. Для тех кто потом захочет использовать Хелпер. CHTML

<li class="@Html.IsSelected(action: "Index", controller: "Comings", status: "true")"><a asp-action="Index" asp-controller="Comings" asp-route-status="true">Открытые закупки</a></li>

1 ответ 1

0

Ответ с английского сайта

RouteData содержит только значения, извлеченные из маршрута. Поэтому, если вы не добавите параметр статуса в маршрут (например, / contractors / {status}), вы не сможете получить значение из данных маршрута.

В этом случае это обычный параметр строки запроса, который вы можете извлечь из:

ViewContext.HttpContext.Request:

var status = html.ViewContext.HttpContext.Request.Query["status"].ToString();

Ваш ответ

Нажимая на кнопку «Отправить ответ», вы соглашаетесь с нашими условиями использования и подтверждаете, что прочитали и поняли наши политику конфиденциальности и нормы поведения.

Всё ещё ищете ответ? Посмотрите другие вопросы с метками или задайте свой вопрос.