This is a migrated thread and some comments may be shown as answers.

Complex object post issues with DateTime property

12 Answers 606 Views
Grid
This is a migrated thread and some comments may be shown as answers.
DominionZA
Top achievements
Rank 1
DominionZA asked on 08 Apr 2013, 09:59 AM
Hi all,

I am having some odd issues with complex objects in my trial version of KendoUI complete for MVC.

I have a base entity class called Entity with the following 2 properties.
public Guid ObjectId { get; set; }
public DateTime CreatedOn { get; set; }

I then have 2 classes, Product and Category. Both inherit from Entity.
Product has a reference to a single category
public Category Category { get; set; }

My grid populates 100%.

The first issue I had was when displaying Category.CreatedOn. I was getting the JSON data instead of a date/time. Searching these forums found me an answer and I have it displaying correctly using the ClientTemplate method on the bound property.
When I make a change and post back the Product.CreatedOn is 100% and contains the value that it had originally in the read request. Category.CreatedOn is set back to 01/01/0001 00:00 which is invalid and causes my ModelState.IsValid check to fail.
This Category.CreatedOn value does however show in the grid correctly if I render the column after applying the ClientTemplate fix above.
If I set a new date in the grid for Category.CreatedOn, then this new value is persisted back to the server on update. The value is only lost if I don't alter it at all.

Am I making sense? Is there a fix for this?

I have been fiddling with DateTime types in the Grid since Thursday last week. Starting to feel real unproductive at the moment so some quick assistance would be appreciated.

TIA
Mike

12 Answers, 1 is accepted

Sort by
0
DominionZA
Top achievements
Rank 1
answered on 08 Apr 2013, 02:23 PM
Been playing around some more. Whole day wasted on this and still not solved. Sigh...

I used
var errors = ModelState.Where(v => v.Value.Errors.Any());
to get a list of the errors on post back and found Category.CreatedOn property was failing validation.
The exception is "The value '/Date(1365085534000)/' is not valid for Created."
AttempedValue is "/Date(1365085534000)/" and culture is "en-ZA".
The CreatedOn value for the product its-self is correct and posting fine.

Why is one posting back fine and the other not? I am close to dumping the CreatedOn field for all entities but know I will just have to revisit this later when I introduce a date field and particularly a date field with the same name across objects and referenced objects.

Please assist. This is killing me now.
0
DominionZA
Top achievements
Rank 1
answered on 09 Apr 2013, 05:59 AM
I cannot believe there is still no response to this from support.
It is a critical problem I am having and consuming a lot of precious development time trying to figure out something that should just work.

Dropping $999 on the suite does not look so enticing anymore. I was prepared to put up with the faults as long as support was good. Oh well...
My client has already paid me the cash for the KendoUI full suite. 3 days left of the trial and was close to licensing. I am seriously reconsidering this position now.

/Off to trial other products.
0
Atanas Korchev
Telerik team
answered on 10 Apr 2013, 11:44 AM
Hi,

 Those issues sound a lot like a known issue caused by not including kendo.aspnetmvc.min.js in your application. Could this be the case? If that file is include the Kendo Grid will never send JavaScript dates as /Date()/. You can find more info here: http://docs.kendoui.com/getting-started/using-kendo-with/aspnet-mvc/introduction#using-kendo-ui-in-asp.net-mvc-3-application or if using ASP.NET MVC 4 http://docs.kendoui.com/getting-started/using-kendo-with/aspnet-mvc/introduction#using-kendo-ui-in-asp.net-mvc-4-application

Regards,
Atanas Korchev
the Telerik team
Join us on our journey to create the world's most complete HTML 5 UI Framework - download Kendo UI now!
0
DominionZA
Top achievements
Rank 1
answered on 10 Apr 2013, 12:14 PM
That file is included.

It is the CreatedOn DateTime property for referenced properties that are being passed back in JSON format.
The CreatedOn property for the primary object is fine and passing back as a Date value.

I gave up on trying to work with my entities in your grid due to the long time it takes to get a response and am using ViewModels to flatten out my objects. So much extra work as the ViewModel's are really just my entity models flattened. Now I have to create ViewModels and map data back and forth between entity model and view model.
0
DominionZA
Top achievements
Rank 1
answered on 10 Apr 2013, 02:50 PM
I have created a small sample app so you can see for yourself what I am talking about.

You will see in the grid there are two date columns.
"Created On" and "Shaft Created On".

If you alter any values EXCEPT for Shaft Created On, the ModelState will be invalid. I have made it throw an exception showing the AttemptedValue and the Key. It is a JSON value.
If however you set any date value for Shaft Created On, then it works fine and the ModelState will not be invalid.

I have 3 days left of my trial. Put some speed on this please.
0
Atanas Korchev
Telerik team
answered on 10 Apr 2013, 03:29 PM
Hello Michael,

I upgraded your project to the current official release 2013.1.319 and I couldn't reproduce the problem. All dates are submitted as strings. I have attached the updated project as well as a screenshot showing how the request looks like at my side.

On a side note I would like to remind you that the guaranteed trial response time is 72 hours (excluding weekends). 


All the best,
Atanas Korchev
the Telerik team
Join us on our journey to create the world's most complete HTML 5 UI Framework - download Kendo UI now!
0
DominionZA
Top achievements
Rank 1
answered on 11 Apr 2013, 06:25 AM
Thanks. Downloading the testapp now to fiddle.

I am unable to use the latest version as a trial. See attached screeny.
Would have gone a long way on my mental state to be able to have used the latest version as a trial and not have to go through this at all.

If the testapp now works as expected, I will go ahead and purchase the full version for $999 this afternoon.
0
Atanas Korchev
Telerik team
answered on 11 Apr 2013, 07:03 AM
Hello Michael,

 What happens when you pick the first option (the one that says 2013.1.319)? Can't you download it from our web site?

All the best,
Atanas Korchev
the Telerik team
Join us on our journey to create the world's most complete HTML 5 UI Framework - download Kendo UI now!
0
Heiko
Top achievements
Rank 1
answered on 21 Jun 2013, 05:51 PM
I have the same problem here. It occurs reproducable when there is the following constellation: entity A has a DateTime property and also a property of entity type B. Entity B has a DateTime property. For example:

public abstract class ModelBase
{
    [DisplayFormat(DataFormatString = "{0:g}")]
    [Editable(false)]
    public DateTime? ModifiedDate { get; set; }
}
  
public class Store : ModelBase
{
    public Store()
    {
        this.Product = new HashSet<Product>();
        this.ProductCategory = new HashSet<ProductCategory>();
    }
  
    [Key]
    public int Id { get; set; }
  
    [Required]
    public string Name { get; set; }
  
    [ScriptIgnore]
    public ICollection<Product> Product { get; set; }
  
    [ScriptIgnore]
    public ICollection<ProductCategory> ProductCategory { get; set; }
}
 
public class Product : ModelBase
{
    [Key]
    public int Id { get; set; }
  
    [Required()]
    public int StoreId { get; set; }
  
    [ForeignKey("StoreId")]
    public Store Store { get; set; }
  
    [Required()]
    [MaxLength(50)]
    public string Name { get; set; }
  
    [Required()]
    public int ProductCategoryId { get; set; }
  
    [ForeignKey("ProductCategoryId")]
    public ProductCategory ProductCategory { get; set; }
}
The problem is NOT the ModelBase - it happens also if I just put ModifiedDate directly into Store entity. Whenever I edit a product and save the results the error occurs as described by DominionZA: kendo tries to set the value of Store.ModifiedDate to something like "/Date(1371765600000)/". See attached screenshot (sorry, german, the error saying "not a valid value"). I certainly use kendo.aspnetmvc.js in my project. This is part of my "RegisterBundles":
bundles.Add(new ScriptBundle("~/bundles/jquery").Include(
            "~/Scripts/kendo/2013.1.514/jquery-1.9.1.js"));
 
bundles.Add(new ScriptBundle("~/bundles/kendo").Include(
            "~/Scripts/modernizr-2.6.2.js",
            "~/Scripts/kendo/2013.1.514/kendo.all.js",
            "~/Scripts/kendo/2013.1.514/kendo.aspnetmvc.js",
            "~/Scripts/cultures/kendo.culture.de-DE.js"
            ));
Any help would be much appreciated.
0
Daniel
Telerik team
answered on 26 Jun 2013, 04:46 AM
Hello Neils,

The dataSource schema does not support complex objects so the type of the "ModifiedDate" will not be known and it will not be converted to a JavaScript date and formatted. You should bind the Grid to a flattened view model which contains the date field in order to avoid the problem.  An alternative would be to use the request Data function and format the date as expected by the model binder:

.Update(update => update.Action("action", "controller").Data("convertDate"))
function convertDate(data) {
    data["Store.ModifiedDate"] = data.Store ? kendo.toString(data.Store.ModifiedDate, "G") : "";
}
Regards,
Daniel
Telerik
Join us on our journey to create the world's most complete HTML 5 UI Framework - download Kendo UI now!
0
Keith
Top achievements
Rank 1
answered on 19 Aug 2015, 01:25 PM

The data function solution worked for me. I found that you have to parse to date before call the tostring however...

.Update(update => update.Action("action", "controller").Data("convertDate"))
function convertDate(data) {
    data["Store.ModifiedDate"] = data.Store ? kendo.toString(kendo.parseDate(data.Store.ModifiedDate, "G")) : "";
}​

0
Sven
Top achievements
Rank 1
answered on 02 Oct 2015, 06:11 PM

Hi,

I've been struggling with the same problem for several hours. It's a little strange that Telerik support staff always suggest to flatten the viewmodel but it's working also without flattening. Only the issue with the date is really annoying.

 

For me, only the following solution worked:

 

<script>
    function convertDate(data) {
        data.PostalAddress.CreatedAt = data.PostalAddress ? kendo.toString(kendo.parseDate(data.PostalAddress.CreatedAt), "G") : "";
    }
</script>

I'm using Kendo UI v.2015.2.805.545

 

Good luck

Sven

 


 
 
Tags
Grid
Asked by
DominionZA
Top achievements
Rank 1
Answers by
DominionZA
Top achievements
Rank 1
Atanas Korchev
Telerik team
Heiko
Top achievements
Rank 1
Daniel
Telerik team
Keith
Top achievements
Rank 1
Sven
Top achievements
Rank 1
Share this question
or