You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

ASP.NET Core重置密码时EmailActiveCode始终返回Null的问题求助

重置密码时EmailActiveCode返回Null的原因及解决方案

核心原因

  1. Get请求参数未传递到ViewModel:访问resetpassword?EmailActiveCode=xxx时,Get方法未接收并将该参数赋值给ResetPasswordViewModel,导致视图无法获取到该值。
  2. 表单未提交EmailActiveCode:视图中没有存储EmailActiveCode的字段,Post表单时仅提交了密码和确认密码,后端自然无法接收到该值。

解决方案

1. 修改ResetPassword的Get方法,接收并传递EmailActiveCode

更新Get方法代码,从QueryString获取EmailActiveCode并赋值给ViewModel:

[Route("ResetPassword")]
[HttpGet]
public IActionResult ResetPassword(string emailActiveCode)
{
    var model = new ResetPasswordViewModel
    {
        EmailActiveCode = emailActiveCode
    };
    return View(model);
}

2. 在视图中添加隐藏字段保存EmailActiveCode

在表单内添加asp-for="EmailActiveCode"的隐藏输入框,确保Post时提交该值:

@model AryanITC.Domain.ViewModels.Account.ResetPasswordViewModel

<form asp-action="ResetPassword" method="post">
    <!-- 密码输入框 -->
    <input asp-for="Password" type="password" class="pass" />
    <span asp-validation-for="Password" class="field-validation-valid col-md-12 text-danger"></span>

    <!-- 确认密码输入框 -->
    <input asp-for="ConfirmPassword" type="password" class="pass" />
    <span asp-validation-for="ConfirmPassword" class="field-validation-valid col-md-12 text-danger"></span>

    <!-- 隐藏字段存储EmailActiveCode -->
    <input asp-for="EmailActiveCode" type="hidden" />

    <button type="submit" class="submit text-center"> submit </button>
</form>

补充说明

ASP.NET Core的模型绑定不会自动将Get请求的QueryString参数延续到后续的Post请求,必须通过ViewModel传递到视图,并通过表单字段(这里用隐藏字段)提交回后端,才能让Post方法的ViewModel正确获取到EmailActiveCode的值。

内容的提问来源于stack exchange,提问作者masoud mazloum

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.17 08:15:43