3

Hello I am a little confused with regards to models in mvc 4 and thought someone may be able to point me in the right direction. This would be most appreciated.

For example if i have a table that has the following fields

        [StringLength(100, ErrorMessage = "The {0} must be at least {2} characters long.", MinimumLength = 5)]         
        public string UserName { get; set; }

        [Required(ErrorMessage="Email Address is Required")]
        [StringLength(15,  ErrorMessage = "Email Address must be between {0} and {1} in size",MinimumLength = 5 )]
        [DataType(DataType.EmailAddress)]
        [Display(Name="Email")]        
        public string Email { get; set; }

        [MaxLength(25)]
        [Display(Name="Mobile Telephone Number")]    
        public string Mobile {get;set;}

        [MaxLength(500)]    
        [Display(Name="Headline")]
        public string Headline {get;set;}

        [Required]
        [StringLength(200)]
        [Display(Name = "First Name")]
        public string FirstName {get;set;}

        [Required]
        [StringLength(200)]
        [Display(Name="Surname")]
        public string Surname { get; set;}

        public virtual int? DayOfBirthId { get; set; }
        public virtual DayOfBirth DayOfBirth { get; set; }
        public virtual int? MonthOfBirthId { get; set; }
        public virtual MonthOfBirth MonthOfBirth { get; set; }
        public virtual int? YearOfBirthId { get; set; }
        public virtual YearOfBirth YearOfBirth{get;set;}

This is my user profile table in the database. However I would like a form that the user registers to the site with. When they first register i do not need all the details such as telephone all i really need is there username, email address and password. Do i create another model for this. Or do i have one model and on the controller set the fields to null or empty string that are not required on registration. I have validation also so would this be set for data that has not been entered on the form.

My question is ultimately should all forms represent models- should the database be redesigned to meet this required. Or should the controller set the values that are not required. Or should there be another model that represents the form be created which maps to this table.

I am a little confused on this and clarification of anyone would be most appreciated.

1 Answer 1

2

It looks like you are mapping your model directly to the view which is fine to do but often the concept of ViewModels are used for this.

Viewmodels are specifically tailored to the views in which they will be used and contain only the properties necessary for the view. ViewModels are then mapped

  1. To models for persisting any changes from the user
  2. From models for showing the user persisted data

You can hand roll the mapping between the two objects or use a tool like AutoMapper or EmitMapper.

Sign up to request clarification or add additional context in comments.

11 Comments

Voting for the AutoMapper.
so i should only include the properties required on the view and keep the models the same- for example if a property of a model-i.e. headline was required on one view say the profile form does this mean i set it to required on the model and not include it on the registration and it will pass validation.
in other words should the model represent the database and the views in terms of forms pick the properties required in each instance- if the validation rules were different, i.e. headline not required to register to the site would this be ommitted from validation if it was not included on the view or does it need to pass the validation of all fields of the model
The validation attributes go onto the ViewModels so that they are specific to the context of the view. Taking your example, HeadLine would go on the ProfileViewModel with a [Required] attribute and the RegistrationViewModel would not have the property. Think of the model as the Domain Model of your application and the ViewModels being specific to displaying data in the model to users of the web application.
so in other words the model is specific to the view- my view must have the properties of the model. For example headline is not required for registration so this should not be in the RegistrationViewModel. I am confused as to whether my models represent the database or the forms. I have a database such as userprofile with registration and userprofile details. Shoudl this be then split up in the database into say registration and userprofile table
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.