Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 4027714
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T11:07:51+00:00 2026-05-20T11:07:51+00:00

I’ve just read about MVC3’s new CompareAttribute that you can apply to a model’s

  • 0

I’ve just read about MVC3’s new CompareAttribute that you can apply to a model’s property to define another property that it should match – the classic use case is confirming email address has been entered correctly, or having a Password and ConfirmPassword field. I’ve run into a problem with trying to implement it in my own project.

We’ve got a fairly standard User object which, among other things, has these properties:

public class User {
    ....
    [Required, RegularExpression(RegularExpressions.Email, ErrorMessage = "Please supply a valid email address")]
    public string EmailAddress

    [Required]
    public string Password
    ....
}

I’ve then incorporated User and a couple of other objects that we need into a view model:

public class SignUpViewModel {
   ....
   public User user { get; set; }
   ....
}

Which, when passed to the form in the UI, allows the ModelBinder to run the Data Annotation validation for the User object as well as the other objects and primitive types in SignUpViewModel when the user submits the form. I was really pleased when this all “just worked” because it meant that we could define validation in just one place and not have too much work to do in persisting out to the database or map UI models to domain models etc.

Noticing that the Html.EditorFor(model => model.User.Password) emits an with the name set to “User.Password”, I added the following to SignUpViewModel:

[Required, Compare("User.Password")]
public string ConfirmPassword { get; set; }

but submitting the form now triggers a validation error along the lines of “Could not find a property named User.Password”. I was hoping that it would follow the same kind of convention, but it would appear not 🙁

I don’t really want to put ConfirmPassword into the User object as ConfirmPassword is purely a UI concern, and it seems wrong to have that in a domain object like that. I also don’t really want to flatten out the various objects within SignUpViewModel as that starts to feel like duplication of where validation rules are defined, and we’re trying to keep our code as DRY as possible.

Has anyone come across a way to get CompareAttribute working with comparing against properties of sibling objects rather than sibling properties as it would appear the attribute expects to?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-20T11:07:52+00:00Added an answer on May 20, 2026 at 11:07 am

    Get rid of the User property on your SignUpViewModel and put the necessary properties from the User domain object on the SignUpViewModel.

    You said you want your concerns separated, and having a domain object as a property on the view model is an example of not having your concerns separated.

    This might seem like it could result in your writing more code, but look into a solution like Automapper to automate mapping your domain object properties to view model properties.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
I am trying to loop through a bunch of documents I have to put
We're building an app, our first using Rails 3, and we're having to build
I'm making a simple page using Google Maps API 3. My first. One marker
I have a bunch of posts stored in text files formatted in yaml/textile (from
I have this code: - (void)parser:(NSXMLParser *)parser foundCDATA:(NSData *)CDATABlock { NSString *someString = [[NSString
I have some data like this: 1 2 3 4 5 9 2 6

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.