我的模型的属性找不到了

问题描述 投票:1回答:3

当我试图在我的项目中重置密码时,它抛出异常。 下面是我的 型号

public class ResetPasswordConfirmModel
{
    public string Toke { get; set; }

    [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; }

    [DataType(DataType.Password)]
    [Display(Name = "confirm new password")]
    [System.Web.Mvc.Compare("Password", ErrorMessage = "The password and confirmation password do not match.")]
    public string ConfirmPassword { get; set; }

账户主管

 [AllowAnonymous]
    [HttpPost]
    public ActionResult ResetPasswordConfirmation(ResetPasswordConfirmModel model)
    {
       if(WebSecurity.ResetPassword(model.Toke,model.NewPassword))
       {
           return RedirectToAction("PasswordResetSuccess");
       }
        return RedirectToAction("PasswordResetFailure");
    }

以下是抛出异常的视图

  @model Champiosnhip.WebUI.Models.ResetPasswordConfirmModel
@{
    ViewBag.Title = "ResetPasswordConfirmation";
}
@using (Html.BeginForm())
{
    @Html.ValidationSummary(true)
    <fieldset>
        <legend>Confrim password reset</legend>
        <div class="label">@Html.LabelFor(t => t.Toke)</div>
        <div>@Html.EditorFor(t => t.Toke)
            @Html.ValidationMessageFor(t => t.Toke)
        </div>
        @Html.Hidden("Toke", Model.Toke)

        <div class="label">@Html.LabelFor(np=>np.NewPassword)</div>
        <div class="field-validation-valid">@Html.EditorFor(np=>np.NewPassword)
            @Html.ValidationMessageFor(np=>np.NewPassword)
        </div>
        <div class="label">@Html.LabelFor(cp=>cp.ConfirmPassword)</div>

      **<div>@Html.EditorFor(cp=>cp.ConfirmPassword)</div>**
            @Html.ValidationMessageFor(cp=>cp.ConfirmPassword) 

Mvc4 异常信息

      The property Champiosnhip.WebUI.Models.ResetPasswordConfirmModel.Password could not be found.

完整的例外情况见 https:/docs.google.comdocumentd13t-9A60sYqFR-gWqwkCSlHE8sovgARy-wie_o8FGhOgedit?usp=分享。

em...我的密码在账户模型中的属性。

 public class RegisterModel
{
    [Required]
    [Display(Name = "User name")]
    public string UserName { get; set; }

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

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

    [Required]
    [DataType(DataType.EmailAddress)]
    [Display(Name = "Email")]
    public string Email { get; set; }

    [Required]
    [DataType(DataType.PhoneNumber)]
    public string Mobile { get; set; }
asp.net-mvc-4
3个回答
19
投票

我也遇到了类似的问题,在开始揪头发之前,我刚刚找到了解决方案.问题是在你的Compare验证属性中,属性仍然命名为 "Password "而不是 "NewPassword"。把它改成下面的样子,一切都会正常工作。

[System.Web.Mvc.Compare("NewPassword", ErrorMessage = "The password and confirmation password do not match.")]

0
投票

"找不到Champiosnhip.WebUI.Models.ResetPasswordConfirmModel.Password的属性。"

该错误表明:(1)在你的视图模型中没有 "密码 "属性。(2)您的视图中的某些东西期望有一个。

在你的视图中,寻找类似...(m => m.Password)的东西。如果它在那里,那么你需要删除它,或者为'密码'字段添加一个属性。


0
投票
public class ResetPasswordConfirmModel
{
public string Toke { get; set; }

[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; }

[DataType(DataType.Password)]
[Display(Name = "confirm new password")]
[System.Web.Mvc.Compare("NewPassword", ErrorMessage = "The password and confirmation password do not match.")]
public string ConfirmPassword { get; set; }
}
© www.soinside.com 2019 - 2024. All rights reserved.