繁体   English   中英

如何有条件地重定向到基于 ASP.NET Core Razor 页面中的单选按钮选择的页面?

[英]How to conditionally redirect to a page based on Radio Button Selection in ASP.NET Core Razor Pages?

我是 ASP.NET Core Razor Pages 的新手。 我已经使用 Visual Studio 2010 和 ASP.NET Core 应用程序的模板构建了一个简单的解决方案

我在登录页面中添加了一个包含两个选项的单选按钮,用于提及性别选择。 我想根据单选按钮中的选定选项有条件地返回 RedirectionToPage。

如果用户选择男孩 + 添加密码和电子邮件 --> 重定向到“./boysNames”页面

如果用户选择 GIRL + 添加密码和电子邮件 --> 重定向到“./girlsNames”页面

这是我的代码:

登录页面中的单选按钮

   <div class="form-group ">
                    <div class="form-check form-check-inline">
                        <input class="form-check-input" type="radio" name="inlineRadioOptions" id="inlineRadio1" value="option1" checked>
                        <label class="form-check-label" for="inlineRadio1">Boy</label>
                    </div>
                    <div class="form-check form-check-inline">
                        <input class="form-check-input" type="radio" name="inlineRadioOptions" id="inlineRadio2" value="option2">
                        <label class="form-check-label" for="inlineRadio2">Girl</label>
                    </div>
                </div>

登录模型:页面模型

//using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Linq;
//using System.Text.Encodings.Web;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Authentication;
using Microsoft.AspNetCore.Identity;
//using Microsoft.AspNetCore.Identity.UI.Services;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.RazorPages;
using Microsoft.Extensions.Logging;

namespace LuckyHandApp.Areas.Identity.Pages.Account
{
    [AllowAnonymous]
    public class LoginModel : PageModel
    {
        private readonly UserManager<IdentityUser> _userManager;
        private readonly SignInManager<IdentityUser> _signInManager;
        private readonly ILogger<LoginModel> _logger;

        public LoginModel(SignInManager<IdentityUser> signInManager, 
            ILogger<LoginModel> logger,
            UserManager<IdentityUser> userManager)
        {
            _userManager = userManager;
            _signInManager = signInManager;
            _logger = logger;
        }

        [BindProperty]
        public InputModel Input { get; set; }

        [BindProperty, Required]
        public string SexSelection { get; set; }




        public IList<AuthenticationScheme> ExternalLogins { get; set; }

        public string ReturnUrl { get; set; }

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

    

        public class InputModel
        {
            [Required]
            [EmailAddress]
            public string Correo { get; set; }

            [Required]
            [DataType(DataType.Password)]
            public string Contraseña { get; set; }

            [Display(Name = "Recordar contraseña")]
            public bool RememberMe { get; set; }
        }

        public async Task OnGetAsync(string returnUrl = null)
        {
            if (!string.IsNullOrEmpty(ErrorMessage))
            {
                ModelState.AddModelError(string.Empty, ErrorMessage);
            }

            returnUrl ??= Url.Content("~/");

            // Clear the existing external cookie to ensure a clean login process
            await HttpContext.SignOutAsync(IdentityConstants.ExternalScheme);

            ExternalLogins = (await _signInManager.GetExternalAuthenticationSchemesAsync()).ToList();

            ReturnUrl = returnUrl;
        }

        public async Task<IActionResult> OnPostAsync(string returnUrl = null)
        {
            returnUrl ??= Url.Content("~/");

            ExternalLogins = (await _signInManager.GetExternalAuthenticationSchemesAsync()).ToList();
        
            if (ModelState.IsValid)
            {
                // This doesn't count login failures towards account lockout
                // To enable password failures to trigger account lockout, set lockoutOnFailure: true  lockoutOnFailure: false
                var result = await _signInManager.PasswordSignInAsync(Input.Correo, Input.Contraseña, Input.RememberMe, lockoutOnFailure: false); //I Think I have to add here the radio selection
                if (result.Succeeded)
                {
                    _logger.LogInformation("BoysSelection.");
                    return RedirectToPage("./boysNames");
                }
                else
                {
                     _logger.LogInformation("Girls Selection.");
                    return RedirectToPage("./girlsNames");
                   
                }
            }

            // If we got this far, something failed, redisplay form
            return Page();
        }
    }
}

您可以尝试在 Pagemodel 中获取单选按钮的值,如果值为option1 ,则return RedirectToPage("./boysNames"); ,如果值为option2 ,则return RedirectToPage("./girlsNames"); . 这是一个演示:

看法:

    <form method="post">
        <div class="form-group ">
            <div class="form-check form-check-inline">
                <input class="form-check-input" type="radio" name="inlineRadioOptions" id="inlineRadio1" value="option1" checked>
                <label class="form-check-label" for="inlineRadio1">Boy</label>
            </div>
            <div class="form-check form-check-inline">
                <input class="form-check-input" type="radio" name="inlineRadioOptions" id="inlineRadio2" value="option2">
                <label class="form-check-label" for="inlineRadio2">Girl</label>
            </div>
        </div>
       
           ...

        <input type="submit" value="submit"/>
    </form>

pagemodel(单选按钮的值将绑定到inlineRadioOptions ):

[BindProperty]
    public string inlineRadioOptions { get; set; }
public async Task<IActionResult> OnPostAsync(string returnUrl = null)
        {
            returnUrl ??= Url.Content("~/");

            ExternalLogins = (await _signInManager.GetExternalAuthenticationSchemesAsync()).ToList();
        
            if (ModelState.IsValid)
            {
                // This doesn't count login failures towards account lockout
                // To enable password failures to trigger account lockout, set lockoutOnFailure: true  lockoutOnFailure: false
                var result = await _signInManager.PasswordSignInAsync(Input.Correo, Input.Contraseña, Input.RememberMe, lockoutOnFailure: false); //I Think I have to add here the radio selection
                if (result.Succeeded)
                {
                    if (inlineRadioOptions == "option1")
                    {
                         _logger.LogInformation("BoysSelection.");
                         return RedirectToPage("./boysNames");
                    }
                    else {
                          _logger.LogInformation("Girls Selection.");
                          return RedirectToPage("./girlsNames");
                   
                    }
                }
            }

            // If we got this far, something failed, redisplay form
            return Page();
        }

暂无
暂无

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

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