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.

1
  • Did you try to use " " (1 space)? Commented Aug 17, 2015 at 12:07

2 Answers 2

1

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

[Required(ErrorMessage = " ")]
public int? TestProperty { get; set; }
Sign up to request clarification or add additional context in comments.

Comments

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.