PDA

View Full Version : [CLOSED] Cannot set the date in a DateFieldFor control



ATLAS
Dec 15, 2013, 10:30 AM
Hi,

There appears to be a bug with the DateFieldFor control which stops you being able to set the date through javascript.

I have a DateFieldFor:


Html.X().DateFieldFor(m => model.DateOfBirth)
.LabelAlign(LabelAlign.Top).FieldLabel("Date of Birth")
.Width(230)
.MaxLength(50)
.Margin(5)
.Format("dd/MM/yyyy")

And I am trying to set the value of field with the following javascript line:


App.DateOfBirth.setValue(user.DateOfBirth);

I have the user.DateOfBirth getting set through an Ajax call, so the DateOfBirth is just a .NET DateTime object.

If DateOfBith on user comes through as a standard .NET DateTime object, the DateFieldFor displays a value in the format 'yyyy-MM-ddThh:mm:ss', which then gives a validation error on the field. If I pre-format the .NET DateTime object in the format 'dd/MM/yyyy' which matches that which the field was setup with, it displays in American date format on the page. Same if format the .NET DateTime object in 'yyyy-MM-dd'. If I format the .NET DateTime object as 'yyyy-dd-MM', then it displays OK on the page, but when saved back to the server, it is in American date format.

Daniil
Dec 16, 2013, 4:45 AM
Hi @ATLAS,

According to
http://docs.sencha.com/extjs/4.2.1/#!/api/Ext.form.field.Date-method-setValue
you should pass to the setValue method a JavaScript Date object or a string in a DataField's Format.

ATLAS
Dec 16, 2013, 7:54 AM
Hi,

I have tried to set the date with a date formatted to a string in the format 'dd/MM/yyyy', and with a date such as 09/12/2013 (9th December 2013), and your date control is changing it to 12/09/2013, 12th September 2013.

Please note the format on DateFieldFor is formatted to 'dd/MM/yyyy'.

Daniil
Dec 16, 2013, 3:43 PM
I cannot reproduce with this test case.

Model

using System;
using Ext.Net.MVC;

namespace Work2MVC.Models
{
public class TestModel
{
[Field(FieldLabel = "DateField")]
public DateTime DateTimeValue
{
get;
set;
}
}
}

View

@model Work2MVC.Models.TestModel

<!DOCTYPE html>
<html>
<head>
<title>Ext.Net.MVC v2 Example</title>
</head>
<body>
@Html.X().ResourceManager()

@Html.X().DateFieldFor(m => m.DateTimeValue).Format("dd/MM/yyyy")

@Html.X().Button().Text("Set").Handler("App.DateTimeValue.setValue('09/12/2013');")
</body>
</html>


Controller Action

public ActionResult Index()
{
return View(new TestModel() { DateTimeValue = DateTime.Now });
}