PDA

View Full Version : [CLOSED] Date Field - MVC/JSON



adelaney
Apr 30, 2012, 3:46 PM
I have a date field in JSON coming back to a grid panel. The data in JSON is returning in this manner:

[Object { RequestID=100000004, TaskName="Prepare Document", TaskDueDate="/Date(1334691950203)/", more...}, Object { RequestID=100000004, TaskName="Notify broker that document is ready", TaskDueDate="/Date(1334692008327)/", more...},


When the data binds with the grid, the date is being displayed is:

/Date(1335374549773)/

However, I have the field datatype set to "Date" and I have the date format:

.Type(ModelFieldType.Date).DateFormat("M/d hh:mmtt")

I did notice in the tooltip help that DateFormat is for dates in the "\date\" format. Do you think my date is showing as it is due to the fact that the case does not match what your code is looking for for a date?

Note: The JSON I am returning is automatically generated and I do not have control over the case of the fields.

Daniil
Apr 30, 2012, 5:52 PM
Hi,

Well, ModelField with Date Type can't parse such string.

"Date(1334691950203)"
and this is not correct format to parse that string

DateFormat("M/d hh:mmtt")

So, if you can't change how this string looks, I can suggest to try the following

Example

<ext:ModelField Name="test">
<Convert Handler="return eval('new ' + value);" />
</ext:ModelField>

Here is a full example.

Example

<%@ Page Language="C#" %>

<%@ Register Assembly="Ext.Net" Namespace="Ext.Net" TagPrefix="ext" %>

<script runat="server">
protected void Page_Load(object sender, EventArgs e)
{
if (!X.IsAjaxRequest)
{
Store store = this.GridPanel1.GetStore();
store.DataSource = new object[]
{
new object[] { "Date(1334691950203)" },
new object[] { "Date(1231334691950)" },
new object[] { "Date(1231394691950)" },
};
store.DataBind();
}
}
</script>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>Ext.NET v2 Example</title>
</head>
<body>
<ext:ResourceManager runat="server" />
<ext:GridPanel ID="GridPanel1" runat="server" AutoHeight="true">
<Store>
<ext:Store runat="server">
<Model>
<ext:Model runat="server">
<Fields>
<ext:ModelField Name="test">
<Convert Handler="return eval('new ' + value);" />
</ext:ModelField>
</Fields>
</ext:Model>
</Model>
</ext:Store>
</Store>
<ColumnModel runat="server">
<Columns>
<ext:DateColumn runat="server" Text="Test" DataIndex="test" Format="yyyy-MM-dd" />
</Columns>
</ColumnModel>
</ext:GridPanel>
</body>
</html>

Daniil
Apr 30, 2012, 7:18 PM
My colleague said that you should use just

DateFormat="M$"

Example

<%@ Page Language="C#" %>

<%@ Register Assembly="Ext.Net" Namespace="Ext.Net" TagPrefix="ext" %>

<script runat="server">
protected void Page_Load(object sender, EventArgs e)
{
if (!X.IsAjaxRequest)
{
Store store = this.GridPanel1.GetStore();
store.DataSource = new object[]
{
new object[] { "/Date(1334691950203)/" },
new object[] { "/Date(1231334691950)/" },
new object[] { "/Date(1231394691950)/" },
};
store.DataBind();
}
}
</script>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>Ext.NET v2 Example</title>
</head>
<body>
<ext:ResourceManager runat="server" />
<ext:GridPanel ID="GridPanel1" runat="server" AutoHeight="true">
<Store>
<ext:Store runat="server">
<Model>
<ext:Model runat="server">
<Fields>
<ext:ModelField Name="test" Type="Date" DateFormat="M$" />
</Fields>
</ext:Model>
</Model>
</ext:Store>
</Store>
<ColumnModel runat="server">
<Columns>
<ext:DateColumn
runat="server"
Text="Test"
DataIndex="test"
Format="yyyy-MM-dd" />
</Columns>
</ColumnModel>
</ext:GridPanel>
</body>
</html>

adelaney
Apr 30, 2012, 7:49 PM
Well, that did work, but it gave me this:
Wed Apr 25 2012 12:22:29 GMT-0500 (Central Daylight Time)

That's nice to see it know how to "read" the data, but now I need it in a display format of:
4/25 12:22:29

Do I need to go back to your original solution or is there something else I should do?

Daniil
Apr 30, 2012, 7:51 PM
You should use also the DateColumn with a respective Format as I did in the both examples.

adelaney
Apr 30, 2012, 7:56 PM
Sorry..using Razor here. I missed that in your sample. That worked as I needed.