Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ErrorMessageResourceName and ErrorMessage cannot both be set, but I'm only setting one

This is the error I'm getting using MVC2:

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

It is coming from the [Required] Validation DataAnnotation.

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've boiled my view model down to this(it breaks any time there is a [Required] attribute, 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 blank...just a submit button.

like image 321
Ryan O'Neill Avatar asked Mar 03 '11 23:03

Ryan O'Neill


1 Answers

In my case, I have write ErrorMessage = null, example :

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

ErrorMessage = " " don't work.

like image 107
Laurentb0204 Avatar answered Sep 19 '22 12:09

Laurentb0204