This is the error I'm getting using MVC2:
E开发者_开发技巧ither 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.
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.
The problem lies in setting the ErrorMessage to a blank string. I would have to assume that at some point there is a check for String.Empty in the MVC code that is causing problems.
Setting the ErrorMessage to " "(one space) solved the issue.
My guess is [DataType(DataType.Text)]
has a validation message defined in resources (in System.ComponentModel.DataAnnotations.Resources) and the existence of that and your [Required]
is causing this InvalidOperationException
. Try Removing [DataType]
or [Required]
and see if the exception goes away.
I had a simmilar issue with a custom ValidationAttribute.
In the IsValid method I was setting the ErrorMessage. The fix was to remove the assignation to the 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 was writting Unit test and they were pasisng only if i was running/debugging one by one. But when I click "Run All", only the first one was passing? They are not Linked in any ways...
I also tried https://stackoverflow.com/a/20848920/4798459 Didn't work either.
So yeah, the fix was to remove the assignation to the ErrorMessage propety!
精彩评论