1

I have a nullable int field as follows. Obviously this is not a required field.

[DisplayName("Previous Job No:")]
public int? previousJobId { get; set; }

At the moment, if user input is invalid, it shows default error message The field Previous Job No: must be a number.

How can I change this default error message using data annotations?

Thanks !

Kaf
  • 33,101
  • 7
  • 58
  • 78
  • possible duplicate of [Providing localized error messages for non-attributed model validation in ASP.Net MVC 2?](http://stackoverflow.com/questions/2480557/providing-localized-error-messages-for-non-attributed-model-validation-in-asp-ne) – nemesv Jun 07 '12 at 14:03

2 Answers2

3

What if you did:

[DataType(DataType.Int32, ErrorMessage = "Custom Error Msg")]
public int? previousJobId { get; set; }

Unfortunately, I am unable to test this atm.

Second Attempt:

This is not pretty, but it will save you from having to create a custom data annotation. Which in turn, saves you from having to write custom jQuery validation. I was able to test this and it worked for me. But, it's up to you if you like the style.

[DisplayName("Previous Job No:")]
[RegularExpression("^[0-9]+$", ErrorMessage = "Custom Error Msg")]
public string previousJobId { get; set; }
private int? _previousJobId2;
public int? previousJobId2
{
    get
    {
        if (previousJobId == null)
        {
            return null;
        }
        else
        {
            return Int32.Parse(previousJobId);
        }
    }
    set
    {
        _previousJobId2 = value;
    }
}

You can test it in the Controller with:

[HttpPost]
public ActionResult Index(HomeViewModel home)
{
    if (ModelState.IsValid)
    {
        int? temp = home.previousJobId2;
    }
    return View(home);
}

You will reference the string in the View

@Html.LabelFor(model =>model.previousJobId)
ScubaSteve
  • 7,724
  • 8
  • 52
  • 65
  • This is clever but unfortunately there is no such DataType as DataType.Int32 – Kaf Jun 07 '12 at 14:12
  • Okay, All I needed was `[RegularExpression("^[0-9]+$", ErrorMessage = "Custom Error Msg")]` and that did the job. Not sure why you put all other stuff. Surely you shown the way. Cheers – Kaf Jun 07 '12 at 15:53
  • Glad it worked for you. When I tried just using the RegularExpression on the int?, any time null was entered, it would not hit the data annotation and would spit out the default error msg. The whole string thing was to get around that. I would be interested to see how you got it to work. – ScubaSteve Jun 07 '12 at 16:02
  • Try adding `@Html.ValidationMessageFor(model => model.previousJobId)` to the view. It wouldn't hit the annotation as it was validated on client but you had not added a client side error message for it. – Kaf Jun 07 '12 at 16:09
0

Try

[Range(1, 1000, ErrorMessage = "Enter a integer value")]
 public int? previousJobId { get; set; }

Or use external project if feasible http://dataannotationsextensions.org/Integer/Create

Here is how they have implemented it: https://github.com/srkirkland/DataAnnotationsExtensions/blob/master/DataAnnotationsExtensions/IntegerAttribute.cs

Abhijit-K
  • 3,569
  • 1
  • 23
  • 31
  • First method is wrong as there is no such annotation called Integer. Second bit obviously validates the range but the non int inputs. – Kaf Jun 07 '12 at 14:16
  • My bad I thought there is something for Min value. Will edit the post. – Abhijit-K Jun 07 '12 at 14:23
  • Oh I remember in one sample project we use it from a library: http://dataannotationsextensions.org/Integer/Create – Abhijit-K Jun 07 '12 at 14:27
  • Here is how it is implemented, easy to write your own from this: https://github.com/srkirkland/DataAnnotationsExtensions/blob/master/DataAnnotationsExtensions/IntegerAttribute.cs – Abhijit-K Jun 07 '12 at 14:30
  • Is this not ok to see usage? I already posted. http://dataannotationsextensions.org/Integer/Create – Abhijit-K Jun 07 '12 at 14:42
  • ah ye that is good mate. sorry I meant how to use the implemented extension. Code ?? – Kaf Jun 07 '12 at 14:45
  • Check this: http://weblogs.asp.net/srkirkland/archive/2011/02/23/introducing-data-annotations-extensions.aspx AND also https://github.com/srkirkland/DataAnnotationsExtensions/blob/master/DataAnnotationsExtensions.Web/Views/Integer/Create.cshtml – Abhijit-K Jun 07 '12 at 14:52