Get the "key" for a strongly typed model in the controller

So, I'm trying to get the key for the model object in the controller to add AddModelErrorto it.

In my opinion, I use

@Html.ValidationMessageFor(model => model.Email)

What is the equivalent code to get a name Keyto add to the controller so that it connects to this ValidationMessage.

+5
source share
2 answers
ModelState.AddModelError("Email", "the email is invalid");

, , . , , , FluentValidation.NET = > , , .

+2

, , HtmlHelpers, :

public static class ModelStateExtensions
{
  public static void AddModelError<TModel>(this ModelStateDictionary dictionary, Expression<Func<TModel, object>> expression, string errorMessage)
  {
    dictionary.AddModelError(ExpressionHelper.GetExpressionText(expression), errorMessage);
  }
}

, :

ModelState.AddModelError<TModel>(i => i.Person.Name, "test");

ModelState.AddModelError("Person.Name", "test");

, Html. MVC , , .

+17

All Articles