A better solution would be to have ViewModels for your Views and Models for your code-first approach. This way your presentation is separate from your domain model.
I'll give you an example. Let's say your custom login page has Username
and Password
text fields, whereas your corresponding code-first model might have the following properties in it:
-Id
-Username
-Password
-DateOfRegistration
-IsApproved
So you wouldn't want to show all of these in a View. That's why you create a ViewModel that contains only properties that pertain to a View (i.e. Username
, Password
).
The domain models (code-first models) go in a Models/
directory, where as ViewModels normally go to ViewModels/
directory.
Your project directory structure might be as follows.
App_Start/
Areas/
Content/
Controllers/
LoginController.cs
Models/
Users.cs
...
Scripts/
Views/
Login/
...
ViewModels/
Login/
LoginViewModel.cs
...
Your Views
would be strongly-typed to a ViewModel.
You can apply attributes specific to a view in a ViewModel. For example, your ViewModel might looks as follows. (Note that ViewModel has view-specific attributes such as Required
, Display
, etc.)
public class LoginViewModel
{
[Required]
[StringLength(30, MinimumLength = 6)]
[Display(Name = "Username")]
public string UserName { get; set; }
[Required]
[StringLength(100)]
[DataType(DataType.Password)]
public string Password { get; set; }
}
And your code-first model might look as follows.
public class Users
{
public int UserId { get; set; }
[StringLength(30)]
public string UserName { get; set; }
[StringLength(20)]
public string Password { get; set; }
public DateTime? DateOfRegistration { get; set; }
public bool IsApproved { get; set; }
}