the following code worked perfectly in MVC 2, but after migrating to MVC 3 it throws an exception of type ArrayTypeMismatchException when executing method modelState.AddModelError
public static void CopyTo(this RulesException ruleException, ModelStateDictionary modelState)
{
foreach (var propertyError in ruleException.Errors)
{
string key = ExpressionHelper.GetExpressionText(propertyError.Property);
var message = propertyError.Message;
modelState.AddModelError(key, message );
}
}
It is a extension method executed after a view not validating. It is call this way
[HttpPost, Authorize]
public ActionResult Create(Entity entity)
{
try
{
//..... perform creation of entity
}
catch (RulesException ex)
{
ex.CopyTo(Controller.ModelState);
}
return View(entity);
}
Any idea? thank you
Stefano
I got the same problem with a similar extension.
Check that the project with your helper is referencing System.Web.MVC 3.0.0.0 and not 2.0.0.0.
To double check that just change the param “ModelStateDictionary” to “object” in your CopyTo function and you should get the correct error message (error in the reference).
Hope it helps