0
votes

I want to return empty string for my validation in ASP.NET MVC 5 project. I have created a property in my view model:

[Required(ErrorMessageResourceType = typeof(TestResources), ErrorMessageResourceName = "Empty_Message", ErrorMessage = null)]
public int? TestProperty { get; set; }

Inside a view:

@Html.ValidationMessageFor(x => x.TestProperty)

When I try this one I get the default message "This field is required.". I want it to be empty in this case, because I add a class to error message's span element that shows up some icon.

I tried something like that:

[Required(ErrorMessage = "")]
public int? TestProperty { get; set; }

...but got an error:

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

2
Did you try to use " " (1 space)? - Andrei

2 Answers

1
votes

Try setting a space. Example "Space". That would solve your problem.

[Required(ErrorMessage = " ")]
public int? TestProperty { get; set; }
0
votes

you can use the following code:

[DisplayFormat(ConvertEmptyStringToNull=false)]

Reference for more details:
RequiredAttribute with AllowEmptyString=true in ASP.NET MVC 3 unobtrusive validation