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

Grid updates using nested / child objects

5 Answers 551 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Aaron
Top achievements
Rank 1
Aaron asked on 05 Sep 2013, 02:57 PM
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!

5 Answers, 1 is accepted

Sort by
0
Victor
Top achievements
Rank 1
answered on 17 Oct 2013, 03:33 PM
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
0
Aaron
Top achievements
Rank 1
answered on 17 Oct 2013, 03:38 PM
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 ....
0
Vladimir Iliev
Telerik team
answered on 22 Oct 2013, 08:04 AM
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!
0
Victor
Top achievements
Rank 1
answered on 22 Oct 2013, 08:21 AM
Hi again,

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

/Victor
0
Pauline
Top achievements
Rank 1
answered on 20 Dec 2013, 09:40 AM
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.

Tags
Grid
Asked by
Aaron
Top achievements
Rank 1
Answers by
Victor
Top achievements
Rank 1
Aaron
Top achievements
Rank 1
Vladimir Iliev
Telerik team
Pauline
Top achievements
Rank 1
Share this question
or