Joseph Woodward
Joseph Woodward

Reputation: 9281

Using a model for multiple purposes

I'm new to ASP.NET MVC using Entity Framework and I'm trying to create a simple login system. At the moment I have UserProfile model that I wish to model a login form off of.

UserProfile.cs

namespace MyProject.Areas.Admin.Models
{
    public class UserProfile {

        [Key]
        public int UserID { get; set; }

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

        [Required]
        [DataType(DataType.Password)]
        [Display(Name = "Password")]
        public string Password { get; set; }

        public string FirstName { get; set; }
        public string Surname { get; set; }
        public string EmailAddress { get; set; }
        public string Telephone { get; set; }

    }
}

As my login form will only require a username and password, is it correct for me to create a separate model (for instance, a LoginModel with just those properties, or should I contine to use the UserProfile model?

It feels better for me to create a separate model to model the login submission, but then I run into the issues such as making them reference the same table?

Thanks

Upvotes: 2

Views: 76

Answers (2)

Raphaël Althaus
Raphaël Althaus

Reputation: 60493

You should have only one Model (Domain model), but different ViewModel class.

The ViewModel will only have the properties (from the Model) needed for a certain View /Action.

To manage mapping between Model and ViewModel(s), you should look at Mapping solutions (like AutoMapper, ValueInjecter...)

Upvotes: 4

paramosh
paramosh

Reputation: 2258

It looks you should distinguish view model and domain model. Interestin discussion was here.

Upvotes: 0

Related Questions