简体   繁体   English

从 _layout 发布的 Asp.net Core Razor 页面

[英]Asp.net Core Razor Pages Post From _layout

What i'm trying to do is change the theme of the application using a check box that is populated as dark or light from the server's session.我想要做的是使用从服务器会话填充为深色或浅色的复选框来更改应用程序的主题。

I know the theme (Style Sheet) can be changed with JavaScript but that leads to loading the default Bootstrap Style Sheet then the dark one which causes the screen to flicker.我知道可以使用 JavaScript 更改主题(样式表),但这会导致加载默认的 Bootstrap 样式表,然后是导致屏幕闪烁的深色样式表。

What I need to do is return the css from the server thought a post method like below.我需要做的是从服务器返回 css 认为像下面这样的 post 方法。

<div>
    <form id="theme-switcher" method="post">
        <div class="custom-control custom-switch">
            <input type="checkbox" class="custom-control-input" asp-for="IsDark" id="theme" />
            <label class="custom-control-label" for="theme">Theme</label>
        </div>
        <button id="change" type="submit" class="btn btn-primary">Change</button>
    </form>
</div>

The code above can be in a view component or a partial view but I can not seem to find a way to post the from.上面的代码可以在视图组件或部分视图中,但我似乎找不到发布自的方法。

_Layout.cshtml _Layout.cshtml

@{
    bool isDark = HttpContext.HttpContext.Session.GetBoolean("IsDark");
}

<!-- Custom styles -->
@if (CultureInfo.CurrentUICulture.Name == "ar-LB")
{
    if (isDark)
    {
        <link rel="stylesheet" type="text/css" href="~/css/site-dark-rtl.css">
    }
    else
    {
        <link rel="stylesheet" type="text/css" href="~/css/site-rtl.css">
    }
}
else
{
    if (isDark)
    {
        <link rel="stylesheet" type="text/css" href="~/css/site-dark.css">
    }
    else
    {
        <link rel="stylesheet" type="text/css" href="~/css/site.css">
    }
}

What I've tied so far is partial views and view components but as far as I've found partial views can not have code behind with OnPost (when adding @page to the partial view I get view data can not be null although the model and the view data are set) and view components can not call methods.到目前为止,我绑定的是部分视图和视图组件,但据我所知,部分视图不能在OnPost后面添加代码(将@page添加到部分视图时,我得到的视图数据不能为空,尽管模型和视图数据设置)和视图组件不能调用方法。

What approach should I use ?我应该使用什么方法?

You can post to different routes, regardless of where you currently are.无论您当前身在何处,您都可以发布到不同的路线。 So assuming you have a Razor page SwitchTheme.cshtml with a code-behind that switches the theme on POST, then you can adjust your <form> tag to post to that page:因此,假设您有一个带有代码隐藏的 Razor 页面SwitchTheme.cshtml ,可以在 POST 时切换主题,那么您可以调整<form>标签以发布到该页面:

<form asp-page="/SwitchTheme" method="post">
    <!-- … -->
</form>

Note the use of the asp-page tag helper to generate the action attribute with a link to the page.请注意使用asp-page标签助手生成带有页面链接的action属性。

For changing things like the design, which doesn't directly have some page content you want to display, you could also use a simple controller that makes the change and then redirects back.对于设计之类的更改,它不直接包含您想要显示的某些页面内容,您还可以使用一个简单的控制器进行更改,然后重定向回来。 Then, you would use the asp-action and asp-controller tag helpers instead:然后,您将改用asp-actionasp-controller标签助手:

<form asp-controller="Utility" asp-action="SwitchTheme" asp-route-returnUrl="@Context.Request.Path" method="post">
    <!-- … -->
</form>
public class UtilityController : ControllerBase
{
    [HttpPost]
    public IActionResult SwitchTheme([FromForm] bool isDark, string returnUrl)
    {
        // switch the theme

        // redirect back to where it came from
        return LocalRedirect(returnUrl);
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM