簡體   English   中英

ASP.net Core Razor 中的登錄模式

[英]Login modal in ASP.net Core Razor Pages

I want to create an asp.net core razor pages (not MVC) program with modal for some of it's functionalities like login, forgot password, etc. I created a razor page for login with bootstrap modal and a link for open modal in frontend and LoginViewModel and后端的注冊服務。 如何使用此頁面訪問所有頁面中的登錄模式? 我可以使用局部視圖嗎? 如何? 否則,解決方案是什么? 這是_登錄:

   public class _LoginModel : PageModel
{
    private readonly ICookieAuthenticationService _cookieAuthenticationService;
    private readonly IUserRegistrationService _userRegistrationService;
    private readonly IUserService _userService;

    public _LoginModel(ICookieAuthenticationService cookieAuthenticationService,
        IUserRegistrationService userRegistrationService,
        IUserService userService)
    {
        _cookieAuthenticationService = cookieAuthenticationService;
        _userRegistrationService = userRegistrationService;
        _userService = userService;
    }

    [BindProperty]
    public LoginViewModel LoginViewModel { get; set; }

    [TempData]
    public string ErrorMessage { get; set; }
    
    ...
}

我從 _Login 中刪除了 @page 並注入服務並將其用於部分視圖:

<partial name="_Login" model="new _LoginModel(cookieAuthenticationService, userRegistrationService, userService, logService)" />

但是由於刪除@page,post方法不起作用。

我想創建一個 asp.net 核心 razor 頁面(不是 MVC)程序,其中包含一些功能,如登錄、忘記密碼等。

部分頁面是沒有@page指令的 Razor 標記文件 (.cshtml),但正如您提到的,如果您從_Login.cshtml中刪除@page指令,這將導致 Razor 頁面功能,例如頁面處理程序等,不能再正常工作了。

要實現在模式彈出窗口中顯示登錄等功能的要求,您可以使用額外的部分頁面_LoginPartial.cshtml ,並將其發布到_Login處理程序,如下所示。

@model _LoginModel

<div class="row">
    <div class="col-md-4">
        <section>
            <form method="post" asp-page="_Login">
                <h4>Use a local account to log in.</h4>
                <hr />
                <div asp-validation-summary="All" class="text-danger"></div>
                <div class="form-group">
                    <label asp-for="LoginViewModel.Email"></label>
                    <input asp-for="LoginViewModel.Email" class="form-control" />
                    <span asp-validation-for="LoginViewModel.Email" class="text-danger"></span>
                </div>
                <div class="form-group">
                    <label asp-for="LoginViewModel.Password"></label>
                    <input asp-for="LoginViewModel.Password" class="form-control" />
                    <span asp-validation-for="LoginViewModel.Password" class="text-danger"></span>
                </div>
                <div class="form-group">
                    <button type="submit" class="btn btn-primary">Log in</button>
                </div>
            </form>
        </section>
    </div>
</div>

調用_LoginPartial

<partial name="_LoginPartial" model='...

更新:

如何在發布到新頁面之前驗證輸入? 諸如空密碼或無效用戶之類的東西。

客戶端驗證可以幫助阻止提交,直到表單有效。 您可以在視圖頁面中使用以下代碼,這將有助於添加支持客戶端驗證的所需腳本引用。

@section scripts{ 
    <partial name="_ValidationScriptsPartial" />
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM