Grid Popup Edit EditorForModel date format ModelState error

1 Answer 367 Views
DateInput Grid
Richard
Top achievements
Rank 4
Iron
Iron
Iron
Richard asked on 01 Apr 2022, 02:51 PM

Afternoon.

I have a MVC Grid with popup editing, created using an example posted on a previous question from me:

https://www.telerik.com/forums/grid-popup-editing-1551548

The popup edit only updates some of the fields in the grid.

I have a datetime field Dateinpost which is set when the record is created - it doesn't need to be updated.

I have set its format in the model:

        [DisplayFormat(ApplyFormatInEditMode = true, DataFormatString = "{0:dd/MM/yyyy}")]
        [DataType(DataType.DateTime)]
        public DateTime? Dateinpost { get; set; }

It doesn't need to appear on the popup template, but I have included it here so that you can see its value:

    <div class="k-edit-field">
        @Html.EditorFor(model => model.Dateinpost)
    </div>

When I update any of the values I get the following ModelState error in the Update action, and the value of Dateinpost in the model is null;

As you can see, the format of the date in AttemptedValue is not correct.

I could write a work around that would clear the ModelState error and not update that field, as it's not to be updated anyway, but it seems that shouldn't be necessary.  I've also tried using a date format of "dd/MM/yyyy HH:mm:ss" but that doesn't work either.

Kind regards,

Richard

1 Answer, 1 is accepted

Sort by
0
Accepted
Tsvetomir
Telerik team
answered on 06 Apr 2022, 08:10 AM

Hi, Richard,

In general, such errors with invalid dates is encountered when the date formats on the client and on the server do not match. This is usually due to a mismatch between the cultures applied.

What I can recommend is that you verify that the cultures are matching. Further information can be found in the following article:

https://docs.telerik.com/aspnet-core/globalization/overview#matching-cultures

 

Regards,
Tsvetomir
Progress Telerik

Virtual Classroom, the free self-paced technical training that gets you up to speed with Telerik and Kendo UI products quickly just got a fresh new look + new and improved content including a brand new Blazor course! Check it out at https://learn.telerik.com/.

Richard
Top achievements
Rank 4
Iron
Iron
Iron
commented on 07 Apr 2022, 04:33 PM

Hi Tsvetomir,

Many thanks for your reply. I've followed the article and done the following: -

Added this line to _Layout.cshtml to use the GB culture file

<script src="~/lib/kendo/js/cultures/kendo.culture.en-GB.min.js"></script>

Added this to the User View which is displaying the model:

<script>
    kendo.culture("en-GB");
</script>

This has done the trick.  Strangely, this is the only View where I've needed to enforce it - all the others were already working with the correct date format.

I didn't need to amend the Configure method of the Startup.cs file and subsequently make the client use the kendo.culture method in order to make the server and client match.  When I queried System.Globalization.CultureInfo.CurrentCulture it seemed to already be set to en-GB.

Thanks again for your help.

Kind regards,

Richard

 

Tsvetomir
Telerik team
commented on 11 Apr 2022, 11:15 AM

Thank you for your feedback! Indeed, the second part of the article that targets amending the server-side culture is needed only when the default culture is different. For instance, if the built-in culture is en-GB, then it would not be needed to change the options.
Tags
DateInput Grid
Asked by
Richard
Top achievements
Rank 4
Iron
Iron
Iron
Answers by
Tsvetomir
Telerik team
Share this question
or