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

[Solved] Child objects not binding correctly

1 Answer 29 Views
Grid
This is a migrated thread and some comments may be shown as answers.
This question is locked. New answers and comments are not allowed.
Steve
Top achievements
Rank 1
Steve asked on 16 Feb 2012, 05:53 PM

I modified this example to show one of the behaviors I am seeing with child objects. 

http://www.telerik.com/community/code-library/aspnet-mvc/grid/batch-editing-with-combobox-editor-template.aspx

A child object for this example is a property that is an object where I want to bind to one or more of it's properties.  The child object I added is Customer as a NewCustomer property.

namespace BatchEditingWithComboBoxEditorTemplate.Models
{
    public class EditableOrder
    {
        public int OrderID { get; set; }
        [UIHint("Employee"), Required]
        public EditableEmployee Employee { get; set; }
        [DataType(DataType.Date), Required]
        public DateTime OrderDate { get; set; }
        [DataType(DataType.Currency), Required]
        public decimal Freight { get; set; }
        public Customer NewCustomer { get; set; }
    }
  
    public class Customer
    {
        public string Name { get; set; }
        public string Address { get; set; }
    }
}


The controller is changed like this:

HomeController.cs:
  
    [GridAction]
        public ActionResult _SelectBatchEditing()
        {
            using (var nw = new NWDataContext())
            {
                List<EditableOrder> list = nw.Orders
                                             .Select(o => new EditableOrder
                                             {
                                                 OrderID = o.OrderID,
                                                 OrderDate = o.OrderDate ?? DateTime.Now,
                                                 Employee = new EditableEmployee { EmployeeID = o.Employee.EmployeeID, FirstName = o.Employee.FirstName, LastName = o.Employee.LastName },
                                                 NewCustomer = new Customer() { Name = "<new>", Address = "<unknown>" },
                                                 Freight = o.Freight ?? 0
                                             })
                                             .ToList();
  
                return View(new GridModel(list));
            }
        }

The view changed like this:

Index.cshtml:
  
         .Columns(columns =>
         {
            columns.Bound(o => o.OrderID).Width(100);
            columns.Bound(o => o.Employee).ClientTemplate("<#= (Employee==null)? '' : (Employee.FirstName + \" \" + Employee.LastName) #>").Width(230);
            columns.Bound(o => o.OrderDate).Width(150);
            columns.Bound(o => o.Freight).Width(220);
            columns.Bound(o => o.NewCustomer.Name).Width(100);
            columns.Bound(o => o.NewCustomer.Address).Width(100);
            columns.Command(commands => commands.Edit()).Title("Edit").Width(200);
         })

You can see on the screen shots the fields are displaying correctly, but when being saved it skips a one of the child properties.

It also will erase values randomly if you have keyboard naviation on and have a child object is bound in this manner.

This issue has come up because the requirement to flatten out every object to a view model makes binding tedious at best.  In a small domain it might not be a problem, but in a domain with hundreds of classes and thousands of properties, this will double our maintainance and slow down development. 

Please let me know if there is a solution.

1 Answer, 1 is accepted

Sort by
0
Daniel
Telerik team
answered on 22 Feb 2012, 04:05 PM
Hello Steve,

I am afraid that this is not supported when using InCell editing. The empty value of the not edited property of the object will override its previous value.

Regards,
Daniel
the Telerik team
Sharpen your .NET Ninja skills! Attend Q1 webinar week and get a chance to win a license! Book your seat now >>
Tags
Grid
Asked by
Steve
Top achievements
Rank 1
Answers by
Daniel
Telerik team
Share this question
or