Click here to Skip to main content
15,912,756 members

Comments by John Mackenzie (Top 3 by date)

John Mackenzie 1-Sep-15 15:40pm View    
Apologies for that Philippe.
John Mackenzie 1-Sep-15 15:39pm View    
I tried your suggestion Alexander, but the error was not trapped at the time the model was changed. The validation is apparently only done when the SaveChanges() function is called.

This was the code I used:

MemberDetail mbr = new MemberDetail();
try
{
mbr.EmailAddress = "thisIsNotAnEmailAddressAndItIsTooLongAnywaySoItShouldCauseAProblemForTwoReasons";
}
catch (System.ComponentModel.DataAnnotations.ValidationException ex)
{
ViewBag.errorMessage = ex.Message;
//throw;
}

The MemberDetail object contains the following definition for EmailAddress:

[Display(Name = "Email Address")]
[DisplayFormat(ConvertEmptyStringToNull = true)]
[EmailAddress]
[StringLength(64, ErrorMessage = "The email address can be up to 64 characters long.")]
public string EmailAddress { get; set; }
John Mackenzie 1-Sep-15 15:05pm View    
Thank you for your suggestions Alexander and Philippe. I tried your suggestion Alexander, but the error was not trapped at the time the model was changed. The validation is apparently only done when the SaveChanges() function is called.

This was the code I used:

MemberDetail mbr = new MemberDetail();
try
{
mbr.EmailAddress = "thisIsNotAnEmailAddressAndItIsTooLongAnywaySoItShouldCauseAProblemForTwoReasons";
}
catch (System.ComponentModel.DataAnnotations.ValidationException ex)
{
ViewBag.errorMessage = ex.Message;
//throw;
}

The MemberDetail object contains the following definition for EmailAddress:

[Display(Name = "Email Address")]
[DisplayFormat(ConvertEmptyStringToNull = true)]
[EmailAddress]
[StringLength(64, ErrorMessage = "The email address can be up to 64 characters long.")]
public string EmailAddress { get; set; }