gpt4 book ai didi

调用 POST 方法时 ASP.NET MVC Model 属性为 null

转载 作者:行者123 更新时间:2023-12-02 10:33:17 26 4
gpt4 key购买 nike

在 Controller 操作中,我进行了以下调用,其中 userId 是已知的非空值:

return View("ChangePassword", new ChangePasswordBindingModel(userId));

以下是 ChangePasswordBindingModel 的定义:

public class ChangePasswordBindingModel
{
private string currentUserId;
[Required]
[DataType(DataType.Password)]
//[Display(Name = "Current password")]
public string OldPassword { get; set; }

[Required]
//[StringLength(100, ErrorMessage = "The {0} must be at least {2} characters long.", MinimumLength = 6)]
[DataType(DataType.Password)]
[Display(Name = "New password")]
public string NewPassword { get; set; }

[Required]
[DataType(DataType.Password)]
//[Display(Name = "Confirm new password")]
[Compare("NewPassword", ErrorMessage = "The new password and confirmation password do not match.")]
public string ConfirmPassword { get; set; }

public ChangePasswordBindingModel()
{

}
public ChangePasswordBindingModel(string userId)
{
this.currentUserId = userId;
}

public string GetUserId()
{
return this.currentUserId;
}
}

以下是绑定(bind)到 ChangePassword 模型的 View :

@model IdentityDevelopment.Models.ChangePasswordBindingModel
@{ ViewBag.Title = "ChangePassword";
}

@Html.ValidationSummary(false)
<h2>Change Password</h2>


@using (Html.BeginForm("ChangePassword","Account", FormMethod.Post))
{
@Html.AntiForgeryToken();
<input type="hidden" name="returnUrl" value="@ViewBag.returnUrl" />

<div class="form-group">
<label>Current Password</label>
@Html.PasswordFor(x => x.OldPassword, new { @class = "form-control" })
</div>

<div class="form-group">
<label>New Password</label>
@Html.PasswordFor(x => x.NewPassword, new { @class = "form-control" })
</div>


<div class="form-group">
<label>Re-enter New Password</label>
@Html.PasswordFor(x => x.ConfirmPassword, new { @class = "form-control" })
</div>

<!-- <button class="btn btn-primary" type="submit">Save</button> -->
<input class="btn btn-primary" type="submit" value="Save" />
}

最后,这是用于在上述表单上单击Save 的 Post 方法:

[HttpPost]
[ValidateAntiForgeryToken]
public async Task<ActionResult> ChangePassword(ChangePasswordBindingModel loginChange)
{
if (ModelState.IsValid)
{
IdentityResult result = null;
try
{
string userid = loginChange.GetUserId();
result = await UserManager.ChangePasswordAsync(userid, loginChange.OldPassword, loginChange.NewPassword);

}
catch (Exception ex)
{

}
if (result != null && result.Succeeded)
{
return RedirectToAction("Index");
}
else
{
if(result != null)
{
AddErrorsFromResult(result);
}

}
}

return View(loginChange);
}

问题是 userid 没有设置为非空值,正如我认为 GetUserId() 会做的那样。为什么发送到 post 方法的 Model 的 currentUserId 属性不包含该值?我通过在调试期间单步调用 GetUserId() 来确认这一点。

最佳答案

首先将 currentUserId 公开,然后将其放入 View 中的隐藏字段中。

 @Html.HiddenFor(x => x.currentUserId )

关于调用 POST 方法时 ASP.NET MVC Model 属性为 null,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39104604/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com