ErrorMessageResourceName and ErrorMessage cannot be set, but I set only one

This is the error I get using MVC2 with:

Either ErrorMessageString or ErrorMessageResourceName must be set, but not both. 

It is based on a [Mandatory] data validation.

Stack trace:

 InvalidOperationException: Either ErrorMessageString or ErrorMessageResourceName must be set, but not both.] System.ComponentModel.DataAnnotations.ValidationAttribute.SetupResourceAccessor() +89338 System.ComponentModel.DataAnnotations.ValidationAttribute.FormatErrorMessage(String name) +38 System.Web.Mvc.<Validate>d__1.MoveNext() +215 System.Web.Mvc.<Validate>d__5.MoveNext() +735 System.Web.Mvc.DefaultModelBinder.OnModelUpdated(ControllerContext controllerContext, ModelBindingContext bindingContext) +424 System.Web.Mvc.DefaultModelBinder.BindComplexModel(ControllerContext controllerContext, ModelBindingContext bindingContext) +732 System.Web.Mvc.ControllerActionInvoker.GetParameterValue(ControllerContext controllerContext, ParameterDescriptor parameterDescriptor) +475 System.Web.Mvc.ControllerActionInvoker.GetParameterValues(ControllerContext controllerContext, ActionDescriptor actionDescriptor) +152 System.Web.Mvc.ControllerActionInvoker.InvokeAction(ControllerContext controllerContext, String actionName) +709 System.Web.Mvc.Controller.ExecuteCore() +162 System.Web.Mvc.<>c__DisplayClass8.<BeginProcessRequest>b__4() +58 System.Web.Mvc.Async.<>c__DisplayClass1.<MakeVoidDelegate>b__0() +20 System.Web.CallHandlerExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute() +453 System.Web.HttpApplication.ExecuteStep(IExecutionStep step, Boolean& completedSynchronously) +371 

I welded the model of my view before that (it is interrupted at any time when there is an attribute [Required], it works fine without):

 [Required(ErrorMessage = "")] [DataType(DataType.Text)] [DisplayName("Property Name")] public string MyProperty { get; set; } 

My controller returns an empty ViewModel and the form in the view is completely empty ... just a submit button.

+8
asp.net-mvc asp.net-mvc-2
source share
4 answers

The problem is setting ErrorMessage to an empty string. I had to assume that at some point there is a check for String.Empty in the MVC code, which causes problems.

Setting ErrorMessage to "" (one place) solved the problem.

+19
source share

In my case, I wrote ErrorMessage = null, for example:

 [EmailAddress(ErrorMessageResourceName = "FORM_FIELD_VALIDATION_EMAIL", ErrorMessageResourceType = typeof(App_GlobalResources.Common.View_Common_Forms), ErrorMessage = null)] 

ErrorMessage = " " does not work.

+20
source share

I assume that [DataType(DataType.Text)] has a validation message defined in resources (in System.ComponentModel.DataAnnotations.Resources), and the existence of this and your [Required] raises this InvalidOperationException . Try removing [DataType] or [Required] and see if the exception is gone.

0
source share

I had a simple problem with a custom ValidationAttribute.

In the IsValid method, I installed ErrorMessage. The fix was to remove the assignment in ErrorMessage propety ...

 protected override ValidationResult IsValid(object value, ValidationContext validationContext) { //code before ... this.ErrorMessage = this.FormatErrorMessage(validationContext.DisplayName); //NOT GOOD... ValidationResult validationResult = new ValidationResult(this.ErrorMessage, //And using the ErrorMessage Here... new[] { validationContext.MemberName }); return validationResult; } 

I wrote Unit test and they were pasisng only if I executed / debugged one by one. But when I click "Run All", only the first drives through? They are not connected in any way ...

I have also tried https://stackoverflow.com/a/312929/

So yes, the fix was to remove the binding to the ErrorMessage program!

0
source share

All Articles