0

Alright, so I have a model that looks like:

public class CustomerViewModel {
  public string Password { get; set; }
}

public class CustomerAddViewModel {
  public CustomerViewModel Customer { get; set; }
  [System.ComponentModel.DataAnnotations.Compare("Customer.Password", ErrorMessage = "The confirm password should match")]
  public string ConfirmPassword { get; set; }
}

I get the error message "Could not find a property named Customer.Password" on validation.

I found this SO Question, but it doesn't apply because in the latest version of validation.unobtrusive, the code looks like this:

element = $(options.form).find(":input[name='" + escapeAttributeValue(fullOtherName) + "']")[0];

where the escapeAttributeValue handles all the valid special characters.

I've tried using System.Web.Mvc.Compare instead, but that causes an error while rendering the view.

Any ideas?

Community
  • 1
  • 1
Manuel
  • 868
  • 1
  • 10
  • 21
  • 1
    You are getting the error because there are no Customer.Password property. There is a Password property but it sits in a different class – Jeandre Pentz Aug 06 '13 at 18:11

2 Answers2

1

For the simple reason that the Property "Customer.Password" does not exist. You can define your ViewModel like this:

public class CustomerAddViewModel {
  public CustomerViewModel Customer { get; set; }
  public string Password 
  {
     get
     {
        return this.Customer.Password;
     }
  }
  [System.ComponentModel.DataAnnotations.Compare("Password", ErrorMessage = "The confirm password should match")]
  public string ConfirmPassword { get; set; }
}
ataravati
  • 8,891
  • 9
  • 57
  • 89
  • It's really that simple? I imagined that validation was "smart" enough to traverse through the object hierarchy. – Manuel Aug 06 '13 at 18:17
  • No, the CompareAttribute only looks for a property with the given name, in the current Model class. – ataravati Aug 06 '13 at 18:24
  • Thanks...I was really beating myself up on this thinking I had some small typo somewhere, when it really was just I had the idea wrong. Thanks for the simple fix! – Manuel Aug 06 '13 at 18:25
  • This works but only server side, it does not compare passwords on client side, can you please highlight on that ? – Ani Shroff Nov 09 '13 at 17:23
  • @AniShroff, this should work on client side as well, with jQuery validation. You don't have to do anything, just make sure client side validation is enabled, and that you are referencing the validation scripts in your View. – ataravati Nov 09 '13 at 17:46
  • @ataravati I tried all the ways but it fails in any one case either server or client, asked [question](http://stackoverflow.com/q/19881480/990677) can you please help. – Ani Shroff Nov 09 '13 at 18:29
0

The proper "MVC" way of doing what you are doing is to avoid having child objects in your View Model. A View Model is used to provide the minimum information that you require for the specific a action. All the information you require to register / create the customer should be in your View Model, and when you submit your form with the valid information, the action that receives it (or somewhere in the data layer depending on your structure) will create a Customer object based on that View Model.

Of course you can probably still bypass what I just said but the longer you refuse to write those extra lines, the hardest it will be to get out of the hole you are digging.

Pluc
  • 2,909
  • 1
  • 22
  • 36
  • This I totally understand. If it was fully under my control, that's the route that I would go, but unfortunately the decision was made that they did not want create the additional view specific models. On the upside, the app will be re-written later on and i'll get to do that! – Manuel Aug 06 '13 at 18:24