视图的输入值没有正确绑定,而隐藏值正在被绑定,没有问题



我正在我的项目中建立一个忘记密码的功能,用户将他们的电子邮件输入到一个表单中,他们会收到一封电子邮件,其中包含一个链接到一个允许他们重置密码的页面。

当表单在下面的ResetPassword视图中提交时,只有隐藏值TokenEmail被正确绑定,而PasswordConfirmPassword显示为空。我也尝试过使用[FromForm],但当我这样做时,所有的值也都为空。在绑定这些值方面,我是否遗漏了什么?

ResetPassword视图:

@using IssueTracker.Models
@model ResetPassword
<h2>Reset Password</h2>
<hr />
<div class="row">
<div class="col-md-12">
<form method="post">
<div asp-validation-summary="All" class="text-danger"></div>
<input asp-for="Token" type="hidden" />
<input asp-for="Email" type="hidden" />
<div class="form-group">
<label asp-for="Password"></label>
<input asp-for="Password" class="form-control" />
<span asp-validation-for="Password" class="text-danger"></span>
</div>
<div class="form-group">
<label asp-for="ConfirmPassword"></label>
<input asp-for="ConfirmPassword" class="form-control" />
<span asp-validation-for="ConfirmPassword" class="text-danger"></span>
</div>
<button type="submit" class="btn btn-primary">Reset</button>
</form>
</div>
</div>

ResetPassword动作方法:

[AllowAnonymous]
[HttpPost]
public async Task<IActionResult> ResetPassword(ResetPassword model)
{
if (ModelState.IsValid)
{
ApplicationUser user = await _userManager.FindByEmailAsync(model.Email);
if (user != null)
{
var result = await _userManager.ResetPasswordAsync(user, model.Token, model.Password);
if (result.Succeeded)
{
return View("ResetConfirmation");
}
foreach (var error in result.Errors)
{
ModelState.AddModelError("", error.Description);
}
return View(model);
}
return View("ResetConfirmation");
}
return View(model);
}

ResetPassword模型类:

using System;
using System.ComponentModel.DataAnnotations;
namespace IssueTracker.Models
{
public class ResetPassword
{
[Key]
[Required]
[EmailAddress]
public string Email { get; set; }
[Required]
[DataType(DataType.Password)]
public string Password { get; set; }
[DataType(DataType.Password)]
[Display(Name = "Confirm password")]
[Compare("Password", ErrorMessage = "Password and Confirm Password must match")]
public string ConfirmPassword { get; set; }
public string Token { get; set; }
public ResetPassword()
{
}
}
}

你试过使用html帮助吗?

@Html.HiddenFor(x => @Model.Token);

更多信息请查看https://www.tutorialsteacher.com/mvc/html-helpers

最新更新