I am novice to MVC framework. I am using entityframework DBcontext to perform Database related operation. I am doing this way Entityframework-->dbcontext-->Model-->Controller--> View. I am directly binding Model to view. e.g. there is Table name UserProfile and i have created Model with same name and colum as property of Model. I am puling collection of record from Userprofile and bind this collection directly to View. Here is Model
[Table("UserProfile")]
public class UserProfile
{
[Key]
[DatabaseGeneratedAttribute(DatabaseGeneratedOption.Identity)]
[DisplayName("ID")]
public long UserId { get; set; }
private string _UserName;
public string UserName
{
get { return _UserName; }
set { _UserName = value; }
}
public string Thalassemic
{
get;
set;
}
[Display(Name = "Your First Name")]
[Required(ErrorMessage = "First Name is Required.")]
[StringLength(30, ErrorMessage = "First Name must be {2}-{1} to long", MinimumLength = 2)]
[RegularExpression("^[a-zA-z ]+$", ErrorMessage = "First name must contain only characters")]
public string FirstName { get; set; }
[Display(Name = "Your Last Name")]
[Required(ErrorMessage = "Last Name is Required.")]
[RegularExpression("^[a-zA-z ]+$", ErrorMessage = "Only characters are allowed.")]
[StringLength(20, ErrorMessage = "First Name must be {2}-{1} to long", MinimumLength = 2)]
public string LastName { get; set; }
[Required(ErrorMessage = "Email Address is Required.")]
[StringLength(250, ErrorMessage = "{0} must be {2}-{1} to long", MinimumLength = 4)]
[Display(Name = "Email Address")]
[DataType(DataType.EmailAddress, ErrorMessage = "Please enter a valid email-address.")]
[RegularExpression("^([a-zA-Z0-9_\\-\\.]+)@((\\[[0-9]{1,3}\\.[0-9]{1,3}\\.[0-9]{1,3}\\.)|(([a-zA-Z0-9\\-]+\\.)+))([a-zA-Z]{2,4}|[0-9]{1,3})(\\]?)$", ErrorMessage = "Please enter a valid email-address.")]
//todo: re-think about updating email address as we are user email as login id and this can't change
public string Email { get; set; }}
Should I use this DTO object to bind data with view
public class UserProfileDTO
{
public long UserId { get; set; }
private string _UserName;
public string UserName
{
get { return _UserName; }
set { _UserName = value; }
}
public string Thalassemic
{ get;
set;
}
public string FirstName { get; set; }
public string LastName { get; set; }
public string Email { get; set; }
}
based on above I want to know that is it right method? if not then What is best practice?