如何根据 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 核心应用程序
的模板构建了一个简单的解决方案
我在登录页面添加了一个包含两个选项的单选按钮,用于选择性别。
我想根据单选按钮中的所选选项有条件地 return RedirectionToPage。
如果用户选择男孩 + 添加密码和电子邮件 --> 重定向到“./boysNames”页面
如果用户选择女孩 + 添加密码和电子邮件 --> 重定向到“./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>
登录模型:PageModel
//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中获取radio button的value,如果value是option1
,return RedirectToPage("./boysNames");
,如果value是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(radio button的值会绑定到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();
}
我是 ASP.NET Core Razor Pages 的新手。 我使用 Visual Studio 2010 和 ASP.NET 核心应用程序
的模板构建了一个简单的解决方案我在登录页面添加了一个包含两个选项的单选按钮,用于选择性别。 我想根据单选按钮中的所选选项有条件地 return RedirectionToPage。
如果用户选择男孩 + 添加密码和电子邮件 --> 重定向到“./boysNames”页面
如果用户选择女孩 + 添加密码和电子邮件 --> 重定向到“./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>
登录模型:PageModel
//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中获取radio button的value,如果value是option1
,return RedirectToPage("./boysNames");
,如果value是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(radio button的值会绑定到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();
}