c# .net MVC4 Model to represent table or form
Posted
by
Matthew Chambers
on Stack Overflow
See other posts from Stack Overflow
or by Matthew Chambers
Published on 2012-12-15T10:48:11Z
Indexed on
2012/12/15
11:03 UTC
Read the original article
Hit count: 141
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.
© Stack Overflow or respective owner