Using 4.2 latest. ASP.NET MVC 5.2 SQL server 2014
I have a simple form that post Person Data. PersonTestEntity has StringLength decoration
..PersonTestEntity..
[DataMember]
[StringLength(5, ErrorMessage = "The FirstName value cannot exceed 5 characters.")]
public virtual System.String FirstName
{
get { return (System.String)GetValue((int)PersonTestFieldIndex.FirstName, true); }
set { SetValue((int)PersonTestFieldIndex.FirstName, value); }
}
Form
@model test.dal.EntityClasses.PersonTestEntity
@Html.ValidationSummary(true)
@using (Html.BeginForm())
{
@Html.EditorForModel()
<p>
<button type="submit">Add Me</button>
</p>
}
Controller
[HttpPost]
public ActionResult PersonAdd(PersonTestEntity p)
{
if (!ModelState.IsValid)
{
return View();
}
return View(p);
}
when i type long text and click "Add Me" i get an exception and does not even go to PersonAdd method.
An exception of type 'System.ArgumentOutOfRangeException' occurred in SD.LLBLGen.Pro.ORMSupportClasses.dll but was not handled in user code
Additional information: The value specified will cause an overflow error in the database. Value length: 8. Column max. length: 5
but when i use Person Class
public class Person
{
[StringLength(5, ErrorMessage = "The FN value cannot exceed 5 characters.")]
public string FirstName { get; set; }
[StringLength(5, ErrorMessage = "The LN value cannot exceed 5 characters.")]
public string LastName { get; set; }
}
and use this to post long test. it does go to PersonAdd method and see the error text on the form.
How can i display ErrorMessage on the form rather than getting an exception?
Thanks