0

I have aps.net MVC project, where I have to set the range of date inputs on user side, I am doing this manually with jquery scripts. Today found html 5 attributes min and max, which work great with restriction date values, the error is shown in @Html.ValidationMessageFor element, but the only trouble is - I cant localize it. Code i have now for my date field:

<td>@Html.EditorFor(m => m.EndDate, new { htmlAttributes = new { @class = datepicker" ", id = "EditorForTillDate", max@="20.12.2018" } })</td>
<td style="width:10%">@Html.ValidationMessageFor(m => m.EndDate, "", new { @class = "text-danger" })</td>

and model:

[Display(Name = "end_date", ResourceType = typeof(Localization))]
[DateWorkExpAttribute(ErrorMessageResourceType = typeof(Localization), ErrorMessageResourceName = "error_end_date_invalid")]
public Nullable<System.DateTime> EndDate { get; set; }

Is it even possible?

GeekyNuns
  • 287
  • 2
  • 20
  • Possible duplicate of [ASP Net MVC - Localization of Validation Messages](http://stackoverflow.com/questions/36353265/asp-net-mvc-localization-of-validation-messages) – krillgar Feb 15 '17 at 13:43
  • How do you get your errors if you use `max` and `min` attributes? Show your whole view with scripts and controller – teo van kot Feb 15 '17 at 13:46
  • @teovankot, `max` and `min` are browser validation attributes. The script doesn't require them to perform validation. Consider this: `` – Charles Feb 15 '17 at 13:50
  • @Charles i know. My question is - what's OP actually try to localize? I don't see anything in code that he posted. That's why i want to see controller and View – teo van kot Feb 15 '17 at 13:53

1 Answers1

0

In ValidationMessageFor will work when in your model addDate annotations for your example will be:

    

[Display (Name = "end_date" ResourceType = typeof (Localization))]
[DateWorkExpAttribute (ErrorMessageResourceType = typeof (Localization), ErrorMessageResourceName = "error_end_date_invalid")]
[StringLength (255 MinimumLength = 3, ErrorMessage = "* Part descriptions must be between 3 and 255 characters in length.")]
[Required (ErrorMessage = "This value is empty !! It's not correctly")]
public Nullable <System.DateTime> EndDate {get; set; }

For Html 5 attribute ValidationMessegeFor will not work because it is only HTML 5 attribute the client site. ValidationMassegeFor will set when controller post method return ModelState.IsValid false (It is mechanism the server side).

Perdido
  • 238
  • 3
  • 12