Complex object post issues with DateTime property

13 posts, 0 answers
  1. DominionZA
    DominionZA avatar
    49 posts
    Member since:
    Jul 2007

    Posted 08 Apr 2013 Link to this post

    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
  2. DominionZA
    DominionZA avatar
    49 posts
    Member since:
    Jul 2007

    Posted 08 Apr 2013 Link to this post

    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.
  3. UI for ASP.NET MVC is VS 2017 Ready
  4. DominionZA
    DominionZA avatar
    49 posts
    Member since:
    Jul 2007

    Posted 09 Apr 2013 Link to this post

    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.
  5. Atanas Korchev
    Admin
    Atanas Korchev avatar
    8462 posts

    Posted 10 Apr 2013 Link to this post

    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!
  6. DominionZA
    DominionZA avatar
    49 posts
    Member since:
    Jul 2007

    Posted 10 Apr 2013 Link to this post

    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.
  7. DominionZA
    DominionZA avatar
    49 posts
    Member since:
    Jul 2007

    Posted 10 Apr 2013 Link to this post

    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.
  8. Atanas Korchev
    Admin
    Atanas Korchev avatar
    8462 posts

    Posted 10 Apr 2013 Link to this post

    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!
  9. DominionZA
    DominionZA avatar
    49 posts
    Member since:
    Jul 2007

    Posted 11 Apr 2013 Link to this post

    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.
  10. Atanas Korchev
    Admin
    Atanas Korchev avatar
    8462 posts

    Posted 11 Apr 2013 Link to this post

    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!
  11. Heiko
    Heiko avatar
    124 posts
    Member since:
    Oct 2012

    Posted 21 Jun 2013 Link to this post

    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.
  12. Daniel
    Admin
    Daniel avatar
    2117 posts

    Posted 25 Jun 2013 Link to this post

    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!
  13. Keith
    Keith avatar
    1 posts
    Member since:
    Apr 2015

    Posted 19 Aug 2015 Link to this post

    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")) : "";
    }​

  14. Sven
    Sven avatar
    10 posts
    Member since:
    Jul 2015

    Posted 02 Oct 2015 in reply to Keith Link to this post

    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

     


     
     
Back to Top
UI for ASP.NET MVC is VS 2017 Ready