【发布时间】:2021-12-17 09:32:26
【问题描述】:
我是 ASP.NET Core Razor 页面的新手。 我使用 Visual Studio 2010 和 ASP.NET Core 应用程序的模板构建了一个简单的解决方案
我在登录页面中添加了一个包含两个选项的单选按钮,涉及性别选择。 我想根据单选按钮中的选定选项有条件地返回 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();
}
}
}
【问题讨论】:
-
使用 javascript (stackoverflow.com/questions/9899921/…) 和
@Url.Page(...)。
标签: c# asp.net asp.net-core razor-pages