I have an MVC Model class that looks like this:
[ValidateNewRegistration]
public class NewRegistrationModel {
[System.ComponentModel.DisplayName("Profile Display Name")]
[Required(ErrorMessage = "Display Name must be specified.")]
[StringLength(50)]
public string DisplayName { get; set; }
[System.ComponentModel.DisplayName("Email Address")]
[Required(ErrorMessage = "Email address must be specified.")]
[RegularExpression("^[A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+\\.[A-Za-z]{2,4}$",
ErrorMessage = "The email address you specified is invalid.")]
[StringLength(100)]
public string EmailAddress1 { get; set; }
}
In my view, I have this:
@Html.ValidationSummary()
@Html.EditorForModel()
I understand how to use the metadata to mark up the attributes, but I want to do is implement my own Editor Template that has two fields in it ... a first name and last name, that will be used in at least 1/2 dozen places in my website.
How do I add error messages so that they appear in the Validation Summary, but in a way that is contained within the EditorTemplate?
Long term what I am looking for is the ability to put a "[UIHint("--Editor Template--")]" into the classes, and have the editor template self-contained enough to issuing it's onw error message for the form field. So that I do not have to add metadata to the class that says [Required...] or [RegularExpression...]. My end goal is a class that looks like this:
[ValidateNewRegistration]
public class NewRegistrationModel {
[System.ComponentModel.DisplayName("Profile Display Name")]
[UIHint("DisplayName")]
public string DisplayName { get; set; }
[System.ComponentModel.DisplayName("Email Address")]
[UIHint("EmailAddress")]
public string EmailAddress1 { get; set; }
[System.ComponentModel.DisplayName("Email Address (confirm)")]
[UIHint("EmailAddress")]
public string EmailAddress2 { get; set; }
}