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

  • SEARCH
  • Home
  • 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 8032061
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T01:16:52+00:00 2026-06-05T01:16:52+00:00

I’m designing a new website with ASP.NET MVC 4 (Beta), VS 11 (Beta), EF

  • 0

I’m designing a new website with ASP.NET MVC 4 (Beta), VS 11 (Beta), EF 5 (Beta), but this question suits for released versions of ASP.NET MVC 3, VS 2010, EF 4, too.

First step: I’m using Entity Framework Code First approach, for example, I’ve got following user model:

public class User
{
  [Key]
  public int UserId {get;set;}

  public String LoginName { get; set; }

  public String Password { get; set; }
}

Now, for registration I need another model, the registration model:

public class Registration
{
  public String LoginName { get; set; }

  public String Password { get; set; }

  public String PasswordConfirm { get; set; }
}

This is where my problems begin: Where should I put my DataValidation Annotations? For example the password should be at minimum 10 characters long and the PasswordConfirmed must match Password and so on. Do I have to write this on every model which could do something with the password (I’m thinking of having a ChangePassword model, too)

Another thing is how to deal with the controller. When I display my Registration ViewModel and everything is fine, do I create a User model and assign the variables from Registration ViewModel to it?

Sometimes I’ve a lot of properties which go to database, but not shown to the user (foreign keys, calculated values etc.).

As thinkink on DRY, I don’t want to repeat my self.

What is the best practice for this one?

To be clear: Annotations isn’t a need. If there a better ways to validate, I will be glad, if you show them.

  • 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-06-05T01:16:53+00:00Added an answer on June 5, 2026 at 1:16 am

    I can’t say objectively which is ‘the best practice’, but here’s how I see it.
    If you’re binding to the view model, verify the view model, so:

    public class Registration
    {
    
        public String LoginName { get; set; }
    
        [Required]
        [StringLength(50, MinimumLength=10)]
        public String Password { get; set; }
    
        [Required]
        [StringLength(50, MinimumLength=10)]
        public String PasswordConfirm { get; set; }
    }
    

    You can either do the validation ‘by hand’ in the controller, check on the POST if the password and confirmation matches, if not add an entry to the ModelState (but that may cause code repetition and is a bit cumbersome) OR use nice IValidatableObject interface on the model:

    public class Registration : IValidatableObject
    {
    
        public String LoginName { get; set; }
    
        [Required]
        [StringLength(50, MinimumLength=10)]
        public String Password { get; set; }
    
        [Required]
        [StringLength(50, MinimumLength=10)]
        public String PasswordConfirm { get; set; }
    
        public IEnumerable<ValidationResult> Validate(ValidationContext context)
        {
            if(Password != PasswordConfirm)
                yield return new ValidationResult("Confirmation doesn't match", new[] {"PasswordConfirm"})
            //etc.
        }
    }
    

    Now with that, when you have your model bound after POST, the validation is done by simply calling ModelState.IsValid, and if it isn’t valid, it returns the list of errors – including your custom errors.

    Now of course you can put the DataAnnotations on the DB-model too as an additional measure, just ‘in case’ to avoid string truncation exceptions etc. if you somehow forgot and tried to push a longer string to the database anyway

    As for the mapping, yes, after you have your model validated, at the end of the POST action you usually you map the properties from the model to either a new User instance (when adding to the DB) or to the existing one for update. You can use AutoMapper or write a naive mapper yourself using reflection – it’s a relatively easy task, but it might be better to leave that as a stand-alone exercise, there is no point in reinventing the wheel.

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

Sidebar

Related Questions

I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I want to count how many characters a certain string has in PHP, but
For some reason, after submitting a string like this Jack’s Spindle from a text
I used javascript for loading a picture on my website depending on which small
this is what i have right now Drawing an RSS feed into the php,
I have this code to decode numeric html entities to the UTF8 equivalent character.
I have a French site that I want to parse, but am running into
I want use html5's new tag to play a wav file (currently only supported

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.