We use cookies to give you the best experience on our website. If you continue to browse, then you agree to our privacy policy and cookie policy. Image for the cookie policy date

Problem with displaying date

Hello,
I have problem with your grid control. My application has a drop down list, from where I choose year. Then with ajax script I'm invoking a action that retrievs all data that have the same year value in DateTime property like that I choosed from the drop down list. The action generates an array with data, converts it to Json and sends it to the grid back. Sadly every date that I load to the grid looks so: 
/Date(1138748400000)/
I checked how the dates retrieved from the database are looking before I return the Json array from the action, and at this point everything looks fine.
This is my grid and drop down list code:

@Html.DropDownListFor(model => model.ChoosenYear, Model.YearsList, "Choose year", new { @class = "form-control", @onchange = "onDropDownChange(this.value)" })

@(Html.EJ().Grid("Grid")
                                                              .Datasource(@Model.GridObjectsList)
                                                              .SelectionType(SelectionType.Multiple)
                                                              .AllowSelection()
                                                              .AllowPaging()
                                                              .Columns(col =>
                                                              {
                                                                  col.Field("Id").HeaderText("Id").TextAlign(TextAlign.Center).IsPrimaryKey(true).IsIdentity(true).Width(10).Add();
                                                                  col.Field("Title").HeaderText("Title").TextAlign(TextAlign.Center).Width(75).Add();
                                                                  col.Field("Date").HeaderText("Date").TextAlign(TextAlign.Center).Width(75).Add();
                                                              })) 

This is the ajax script code:

function onDropDownChange(args) {
            $.ajax({
                url: "/Controller/GetDataByYear",
                type: "POST",
                data: { year: args },
                success: function(result) {
                    var obj = $("#Grid").ejGrid("instance");
                    obj.dataSource(result);
                }
            });
        }

And this is my action:

public JsonResult GetDataByYear(string year)
        {
            var selectedYear = new DateTime(Convert.ToInt32(year), 1, 1);
            var jsonResult = Json(
                _dbService.Get().Where(p =>  p.Date.Year == selectedYear.Year).Select(p => new GridObject
                {
                    Id = p.Id,
                    Date = p.Date,
                    Title = p.Title 
                }).ToList());
           return jsonResult; // at this point Dates look normal, that means in yyyy-MM-dd format
        }

Can you help me with this problem?

Regards,
Roman Suska

1 Reply

MF Mohammed Farook J Syncfusion Team October 1, 2014 12:03 PM UTC

Hi Roman,

 

Thanks for using Syncfusion products.

 

We have analyzed the code provided by you, and achieved your requirement by using “ej.parseJSON and the column Format”.

.

 

 

The column format property is used to convert the user format in Grid columns. Please find the code snippet.

 

[In view]

 

@(Html.EJ().DropDownList("drpdown").ClientSideEvents(c=> c.Change("change")).Datasource(ViewBag.data))

@(Html.EJ().GridOrdersView>("Grid")

        .Datasource((IEnumerable<object>)ViewBag.datasource)

              

                   .EditSettings(e => { e.AllowAdding().AllowEditing(); })

                   .ClientSideEvents(c=> c.EndEdit("EndEdit"))

        .Columns(col =>

        {

           

            col.Field("OrderDate").HeaderText("Order Date").TextAlign(TextAlign.Right).Width(80).Format("{0:MM/dd/yyyy}").Add();

           

        }))

 

Since we are using ej.parseJSON method, we need to serialize the JSON Value after AJAX post success method. ej.parseJSON method helps to serialize the JSON Value in proper way

Please find code snippet.

 

[in ajax post]

 

<script type="text/javascript">

    function change(args) {

        var res = JSON.stringify(args)

        $.ajax({

            url: "Home/Data",

            type: "POST",

            dataType:"json",

            data: { data: res },

            success: function(result) {

                var obj = $("#Grid").ejGrid("instance");

                obj.dataSource(ej.parseJSON(result.Data));

            }

        });

    }

   

script>

 

 

[in controller]

  public ActionResult Index()

        {

            var drpdata = new NorthwindDataContext().OrdersViews.Select(c => c.OrderDate).Distinct().ToList();

            ViewBag.data = drpdata;

            var DataSource = new NorthwindDataContext().OrdersViews.ToList();

            ViewBag.datasource = DataSource;

            return View();

        }

        public ActionResult Data(string data)

        {

            

            JavaScriptSerializer datajson = new JavaScriptSerializer();

            var obj = datajson.Deserialize<DropDownListProperties>(data);

           

            var jsonResult = Json(new NorthwindDataContext().OrdersViews.ToList());

            return Json(jsonResult, JsonRequestBehavior.AllowGet);

               

 

        }

 

 

For your convenience We have created a sample and the same can be downloaded from the below link:

 

Sample Location:

 

http://www.syncfusion.com/downloads/support/directtrac/general/Sample_117336-1653305310.zip

 

Please let us know if have any other queries,

 

Regards,

J.Mohammed Farook


Loader.
Live Chat Icon For mobile
Up arrow icon