As  Lk
As Lk

Reputation: 19

The property of my model couldnot be found

When i try to reset password in my project it throw exception. Here is my Model

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

Account Controller

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

Here is view which throw exception

  @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 Exception information

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

Entire exception could be found at https://docs.google.com/document/d/13t-9A60sYqFR-gWqwkCSlHE8sovgARy-wie_o8FGhOg/edit?usp=sharing

em.. my password porperty in Account Model

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

Upvotes: 1

Views: 3732

Answers (3)

Muhammad Faiq
Muhammad Faiq

Reputation: 69

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

Upvotes: 0

Oliver Siebenhaar
Oliver Siebenhaar

Reputation: 201

I had a similar problem and just found the solution before starting to pull my hair out. The problem is that in your Compare validation attribute the property is still named "Password" and not "NewPassword". Change it as below and everything will work fine:

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

Upvotes: 20

monkeyhouse
monkeyhouse

Reputation: 2896

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

The error indicates that (1) there is no 'Password' property in your view model. (2) Something in your view expects one.

In your view, look for something like ...(m => m.Password). If it is there, then you either need to eliminate it, or add a property for the 'Password' field.

Upvotes: 0

Related Questions