Grid updates using nested / child objects

6 posts, 0 answers
  1. Aaron
    Aaron avatar
    5 posts
    Member since:
    Apr 2011

    Posted 05 Sep 2013 Link to this post

    I have an older copy of the .net kendo ui sample code.  In the sample, the following ViewModel is included.

        public class ProductViewModel
        {
            public ProductDTO ProductMeta { get; set; }
            public int ProductID { get; set; }
            public string ProductName { get; set; }
            public decimal UnitPrice { get; set; }
            public int UnitsInStock { get; set; }
            public bool Discontinued { get; set; }
            public DateTime LastSupply { get; set; }
            public int UnitsOnOrder { get; set; }
        }

    The VM also uses this class.

        public class ProductDTO
        {
            public int ProductID { get; set; }
            public string ProductName { get; set; }
            public decimal? UnitPrice { get; set; }
        }

    In the Areas/razor/views/grid/editing.cshtml file, if I change the following line

    @(Html.Kendo().Grid<Kendo.Mvc.Examples.Models.ProductViewModel>()
        .Name("Grid")
        .Columns(columns =>
        {
            columns.Bound(p => p.ProductName);
          columns.Bound(p => p.UnitPrice).Width(140);        

    TO....

    @(Html.Kendo().Grid<Kendo.Mvc.Examples.Models.ProductViewModel>()
        .Name("Grid")
        .Columns(columns =>
        {
            columns.Bound(p => p.ProductName);
          columns.Bound(p => p.ProductMeta.UnitPrice).Width(140);


    The binding works correctly on update.  So if I change the value of unit price to 25, the vm.ProductMeta.UnitPrice value will be set appropriately to 25.

    However, if I add the following statement (statement bolded), the binding no longer works

        .DataSource(dataSource => dataSource
            .Ajax()
            .Batch(true)
            .ServerOperation(false)
            .Events(events => events.Error("error_handler"))
            .Model(model =>
                {
                    model.Id(p => p.ProductID);
                  model.Field(p => p.ProductMeta.UnitPrice).DefaultValue(30);
                })

    Inspecting the payload sent to the GridController.EditingUpdate() method, I notice that the value for ProductMeta.UnitPrice is not changed.

    Is there a work around for this?  

    It seems like the grid is not overly fond of working with nested objects exposed from VMs, especially when using the Model() method.

    I'd rather not have to flatten my VM in order to get this to work.

    Thanks!
  2. Victor
    Victor avatar
    167 posts
    Member since:
    Nov 2010

    Posted 17 Oct 2013 Link to this post

    Interesting, we are also trying to work with nested properties similair to yours. Did you resolve this issue and if so, would you share how?

    /Victor
  3. Aaron
    Aaron avatar
    5 posts
    Member since:
    Apr 2011

    Posted 17 Oct 2013 Link to this post

    Final word was that it is not supported, so we have to flatten our VM's, unfortunately.  HUGE design flaw, in my opinion.  Having to write bloated software in order to work with a third party's tooling is a HUGE no-no.  We almost ditched the grid because of this.  We would have too, if we weren't under such strict timelines.  As you know, grids come with a significant learning curve, especially when you need to do anything outside of a demo ....
  4. Vladimir Iliev
    Admin
    Vladimir Iliev avatar
    2192 posts

    Posted 22 Oct 2013 Link to this post

    Hi Aaron,

     
    Please note that you can set the default value of nested property in the following way:

    .DataSource(dataSource => dataSource
        .Ajax()
            .Model(model => {
                model.Id(p => p.ProductID);
                model.Field(m => m.ProductID).Editable(false);
                //define the default value of nested property by creating bew instance of parent model
                model.Field(p => p.ProductMeta).DefaultValue(new ProductMetaViewModel() { UnitPrice = 25 });
    Regards,
    Vladimir Iliev
    Telerik
    Join us on our journey to create the world's most complete HTML 5 UI Framework - download Kendo UI now!
  5. Victor
    Victor avatar
    167 posts
    Member since:
    Nov 2010

    Posted 22 Oct 2013 Link to this post

    Hi again,

    We ended up using the DefaultValue which works, but nested viewmodels certainly needs more work than normal ones.

    /Victor
  6. Pauline
    Pauline avatar
    7 posts
    Member since:
    Sep 2013

    Posted 20 Dec 2013 Link to this post

    Hi Aaron,

    You might also try inspecting the request headers and creating a custom model binding for your data.
    References:
    http://msdn.microsoft.com/en-us/magazine/hh781022.aspx
    http://blog.thekfactor.info/posts/tag/imodelbinder/

    Worked for me.

Back to Top