Date formatting is strange in editor
Date formatting is strange in editor

I have two datatable editors. Both need to return dates in Dutch format, edit etc.
This is the C# code for one table:
[HttpGet]
[HttpPost]
public ActionResult LeningenTable([FromForm]string GebouwID = "-1")
{
using (var db = new Database("azure", _configuration.GetConnectionString("BBGroepDataContext")))
{
var editor = new Editor(db, "Leningen", "Id")
.Model<LeningViewModel>()
.Field(new Field("Leningverstrekker")
.Validator(Validation.NotEmpty(new ValidationOpts { Message = "De naam van de leningverstrekker is verplicht." }))
)
.Field(new Field("StartDatum")
.Validator(Validation.NotEmpty(new ValidationOpts { Message = "Voer een geldige datum in." }))
.GetFormatter(Format.DateSqlToFormat("dd/MM/yyyy"))
.SetFormatter(Format.DateFormatToSql("dd/MM/yyyy"))
)
.Field(new Field("EindDatum")
.Validator(Validation.NotEmpty(new ValidationOpts { Message = "Voer een geldige datum in." }))
.GetFormatter(Format.DateSqlToFormat("dd/MM/yyyy"))
.SetFormatter(Format.DateFormatToSql("dd/MM/yyyy"))
)
.Field(new Field("Hoofdsom")
.Validator(Validation.NotEmpty(new ValidationOpts { Message = "Voer een getal in." }))
.Validator(Validation.Numeric("nl-NL", new ValidationOpts { Message = "Voer een getal in." }))
)
.Field(new Field("Aflossingspercentage")
.Validator(Validation.NotEmpty(new ValidationOpts { Message = "Voer een getal in tussen de 0 en 100." }))
.Validator(Validation.MinMaxNum(0, 100, new ValidationOpts { Message = "Voer een getal in tussen de 0 en 100." }))
)
.Field(new Field("Voorwaarden")
.Validator(Validation.NotEmpty(new ValidationOpts { Message = "Voer een getal in." }))
)
.Field(new Field("GebouwId")
.SetValue(GebouwID)
)
.Where("gebouwId", GebouwID);
editor.PostCreate += (sender, e) => _LogChange(db, "Leningen", "create", e.Id, e.Values);
editor.PostEdit += (sender, e) => _LogChange(db, "Leningen", "edit", e.Id, e.Values);
editor.PostRemove += (sender, e) => _LogChange(db, "Leningen", "remove", e.Id, e.Values);
var response = editor.Process(Request).Data();
return Json(response);
}
}
When I look at the response I see this for the dates:
StartDatum: {6-6-2020 00:00:00}
However. The datatable in the client view shows: 2020-06-06T00:00:00
I also have this code in the same controller:
```
[HttpGet]
[HttpPost]
public ActionResult HuurdersTable([FromForm]string GebouwID = "-1")
{
using (var db = new Database("azure", _configuration.GetConnectionString("BBGroepDataContext")))
{
var editor = new Editor(db, "Huurders", "id")
.Model<HuurderViewModel>("Huurders")
.Field(new Field("NaamHuurder")
.Validator(Validation.NotEmpty(new ValidationOpts { Message = "De naam van de huurder is verplicht." }))
)
.Field(new Field("Huisnummer")
.Validator(Validation.NotEmpty(new ValidationOpts { Message = "Het huisnummer is verplicht." }))
)
.Field(new Field("HuurcontractHuurder")
.Validator(Validation.NotEmpty(new ValidationOpts { Message = "Het Huurcontract is verplicht." }))
)
.Field(new Field("StartDatumContract")
.Validator(Validation.NotEmpty(new ValidationOpts { Message = "Voer een geldige datum in." }))
.GetFormatter(Format.DateSqlToFormat("dd/MM/yyyy"))
.SetFormatter(Format.DateFormatToSql("dd/MM/yyyy"))
)
.Field(new Field("EindDatumContract")
.Validator(Validation.NotEmpty(new ValidationOpts { Message = "Voer een geldige datum in." }))
.GetFormatter(Format.DateSqlToFormat("dd/MM/yyyy"))
.SetFormatter(Format.DateFormatToSql("dd/MM/yyyy"))
)
.Field(new Field("Opzegtermijn")
.Validator(Validation.NotEmpty(new ValidationOpts { Message = "Voer een getal in." }))
.Validator(Validation.Numeric("nl-NL", new ValidationOpts { Message = "Voer een getail in." }))
)
.Field(new Field("AantalM2VVO")
.Validator(Validation.NotEmpty(new ValidationOpts { Message = "Voer een getal in." }))
.Validator(Validation.Numeric("nl-NL", new ValidationOpts { Message = "Voer een getail in." }))
)
.Field(new Field("AantalM2BVO")
.Validator(Validation.NotEmpty(new ValidationOpts { Message = "Voer een getal in." }))
.Validator(Validation.Numeric("nl-NL", new ValidationOpts { Message = "Voer een getail in." }))
)
.Field(new Field("GebouwId")
.SetValue(GebouwID)
)
.Where("GebouwId", GebouwID)
.MJoin(new MJoin("HuurderExtraEigenschappen")
.Link("Huurders.id", "HuurderExtraEigenschappen.HuurderId")
.Field(new Field("id"))
)
;
editor.PostCreate += (sender, e) => _LogChange(db, "Huurders", "create", e.Id, e.Values);
editor.PostEdit += (sender, e) => _LogChange(db, "Huurders", "edit", e.Id, e.Values);
editor.PostRemove += (sender, e) => _LogChange(db, "Huurders", "remove", e.Id, e.Values);
var response = editor.Process(Request).Data();
return Json(response);
}
}
```
For this code the response shows: StartDatumContract: "02-06-2020"
And on the client side everything is fine.
Please help.
This question has an accepted answers - jump to answer
Answers
UPDATE: It got stranger.
I found out that it doesn't work because i didn't specify the table name
.Model<LeningViewModel>() (Line 9)
However. I also have code where results of a datatable are based on a selected row of another datatable. If I specify the tablename in the .Model, the date is correct, but the parent child function breaks.
Also, when I have fields in this form (Look at Winstuitkeringen.Uitkeringsdatum):
The date is displayed incorrect
Hi,
Can you show me the JSON that is returned from the server when the table is initially loaded please? The browser’s Network Inspector will be able to show you that.
Thanks,
Allan
This is without the table name:
This is if I specify the table name
Hi,
I'm not seeing
Uitkeringsdatum
in the JSON above I'm afraid. Are you able to show the entire JSON string, rather than just part of it (some of it is cut off with ellipsis).Regarding this point, I'd probably need a link to a page showing that issue to be able to help I think. Are there any Javascript errors shown?
Thanks,
Allan
This first screenshot is what I get when i use the following code in my controller:
.Model<ParticipantViewModel>()
The parent child function works, but the dates are wrong.
If I change this into:
.Model<ParticipantViewModel>("Participanten")
You get the screenshot below with the right dates, but the parent child function doesn't work anymore.
This is the JSON from the table at the bottom in the first screenshot.
There are no javascript errors when the parent child function breaks.
It looks like this function in the JavaScript code doesn't pass any value when I set the table name in the controller
My guess is that with the table name added on the server-side you also need to add it on the client-side - for example:
might need to become:
If it isn't that, can you show me your Javascript code please?
Allan
Ok, so this solved part of the problem. Thanks for that. The problem still remaining is as follows:
In this screenshot we have a table with a lookup table
The Javascript code is:
The controller code (C#) is:
As you can see the .Setformatter doesn't work.
It looks like it is the
GetFormatter
that isn't working.Could you show me the
InvesteringViewModel
? Coudl you also show me the JSON response from the server for that API end point?Thanks,
Allan
This is the response:
And this is the viewmodel
No question that the get formatter isn't working.
could you change the
DateTime
to be juststring
please? I think that might be the issue.Thanks,
Allan
This works for the date, however the decimal format is still not set correctly. I aslso use the get formatter to show numbers with a comma as decimal seperator.
You are using
$.fn.dataTable.render.number('.', ',', 2, '€')
on the client-side, so I would suggest removing the server-side formatter for your decimal numbers.Allan
But that code is only for the table. The editor form doesn't take that code.
The users must be able to edit the numbers in Dutch format (comma as decimal)
If you use a getFormatter with the correct format server side and use no formatter whatsoever client side, it works! Regardless of whether it is a Data Table or Editor. I support two languages, English (UK) and German (Germany). This concept works for numbers, dates and what have you. And Dutch numbers are the same as German numbers ...
What getformaater do you use on the server side? My problem is that when you have fields on the server side in the form of table.property it doesn't work. I removed the client side formatter.
This is my server side code but when i open the edior window i still get a number wih 55.10 in stead of 55,10. The table is showing the correct format.
.Field(new Field("Kosten.Bedrag") Is the problem.
Can you give me an example of your solution for German?
If I change from decimal to string type in the model, the editor is correct but the table is wrong. I'm lost
Sure, but my server language is PHP. But it should be very similar for other languages. I use a custom formatter not one of the built-in ones. (That is not for a special reason just because I found it simpler ... no docs to read
)
For German:
The getFormatter converts 1000000.99 into 1.000.000,99
The setFormatter converts 1.000.000,99 into 1000000.99
For English:
The getFormatter converts 1000000.99 into 1,000,000.99
The setFormatter converts 1,000,000.99 into 1000000.99
I use language dependent field masking at the Editor front end to make sure the values sent from the client have the expected German or English format.
Here are the get and set formatters for English and German:
Maybe you can do the setFormatter a bit more elegantly. It was one of my first PHP programming tasks a couple of years ago ...
Thanks! I think a couple of problems are in the net library because I have to change every type in my model to string
Heard of that before. You might find something about this by searching the forum. I noticed that Editor sends everything as strings but it didn't really cause any trouble for me using PHP with MySQL. (MySQL doesn't mind getting strings for numeric fields either - as long as they have the correct format. All inserts and updates work fine.)