I am developing a client-side and server-side validation for a certain viewModel property.
In the .cshtml file I put this:
@Html.DropDow
You could write an extension method that will take a lambda expression for the key instead of a string:
public static class ModelStateExtensions
{
public static void AddModelError(
this ModelStateDictionary modelState,
Expression> ex,
string message
)
{
var key = ExpressionHelper.GetExpressionText(ex);
modelState.AddModelError(key, message);
}
}
and then use this method:
catch (BusinessException e)
{
ModelState.AddModelError(
x => x.EntityType.ParentId,
Messages.CircularReference
);
}