Question

Is it possible to remove property name form the validation message? For example, instead of:

Field 'Name' should not be empty.

I want to show:

Field should not be empty.

I need to do this global, for all validators.

Était-ce utile?

La solution

You can do this using the localization customization like so to make the change globally. You can then of course override specific errors with a custom format if you need a one-off change.

ValidatorOptions.ResourceProviderType = typeof(MyResources);

...

public class MyResources {
   public static string notempty_error {
      get { 
          return "Field should not be empty.";
      }
   }
}

Autres conseils

easiest way would be to pass a custom message. You can also override it so it always uses that message.

[Required(ErrorMessage = "Field should not be Empty")]
public string Name { get; set; }
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top