Is there a way to implement the idea of a data domains (at a property level) inside of a class that is used as a model in a view in ASP.Net MVC 4?
Consider this code:
public class LoginProfileModel {
[DisplayName("Login ID")]
[Required(ErrorMessage = "Login ID is required.")]
public string LogonID { get; set; }
[DisplayName("Password")]
[Required(ErrorMessage = "Password cannot be blank.")]
[StringLength(20, MinimumLength = 3)]
[DataType(DataType.Password)]
public string Password { get; set; }
}
Here is a LoginProfileModel in for ASP.Net MVC 4. It uses a variety of metadata/data annotations so that I can create a clean view with this code:
@model myWebSite.Areas.People.Models.LoginProfileModel
@using ( Html.BeginForm( "Index" , "Login" ) ) {
@Html.ValidationSummary()
@Html.EditorForModel()
<input type="submit" value="Login" />
}
I use the idea of a "Login ID" and a "Password" in more than one view, and therefore, in more than one view model. I want to be able to define the attributes that a Password uses, or possibly the Password itself with all of it's data annotations in a single location so that I can reuse all those definitions where needed, rather than respecifying them every time they are used:
[DisplayName("Password")]
[Required(ErrorMessage = "Password cannot be blank.")]
[StringLength(20, MinimumLength = 3)]
[DataType(DataType.Password)]
public string Password { get; set; }
Is that possible some way?