无法找到我的模型的属性

时间:2013-06-27 22:11:40

标签: asp.net-mvc-4

当我尝试在项目中重置密码时会引发异常。     这是我的模型

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.com/document/d/13t-9A60sYqFR-gWqwkCSlHE8sovgARy-wie_o8FGhOg/edit?usp=sharing 找到整个例外

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

3 个答案:

答案 0 :(得分:19)

我遇到了类似的问题,在开始拔头发之前就找到了解决方案。 问题是,在您的比较验证属性中,该属性仍然被命名为“密码”而不是“NewPassword”。如下更改它,一切都会正常工作:

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

答案 1 :(得分:0)

  

“无法找到属性Champiosnhip.WebUI.Models.ResetPasswordConfirmModel.Password。”

错误表示(1)视图模型中没有“密码”属性。 (2)你认为有些东西需要一个。

在您看来,寻找类似......(m =&gt; m.Password)的内容。如果它存在,那么您需要消除它,或者为“密码”字段添加属性。

答案 2 :(得分: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; }
}