0

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.

Nickon
  • 9,652
  • 12
  • 64
  • 119

2 Answers2

1

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

[Required(ErrorMessage = " ")]
public int? TestProperty { get; set; }
Vishal
  • 127
  • 9
0

you can use the following code:

[DisplayFormat(ConvertEmptyStringToNull=false)]

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

Community
  • 1
  • 1
Biswabid
  • 1,378
  • 11
  • 26