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

Batch Grid Editing and Complex Nested Models

0 Answers 118 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.
dejavu
Top achievements
Rank 2
dejavu asked on 26 May 2011, 04:10 PM
I will try to be as clear as possible.

Problem is i have two models, as:

public sealed class ProductModel
{
    public int IdProduct
    {
        get;
        set;
    }
 
    [UIHint("Category")]
    public CategoryModel Category
    {
        get;
        set;
    }
 
    public string Code
    {
        get;
        set;
    }
 
    public string Description
    {
        get;
        set;
    }
}

And then:

public sealed class CategoryModel
{
    public int IdCategory
    {
        get;
        set;
    }
 
    public string Code
    {
        get;
        set;
    }
 
    public string Description
    {
        get;
        set;
    }
}

So, the Grid i've been using implements as edit mode Batch Editing. Values are displayed just fine. Click on Category column reveals the Category Edit template, implemented as:

@(Html.Telerik().ComboBox()
    .Name("Category")
    .DataBinding(binding => binding
        .Ajax()
        .Select("Categories", "Home")))

Products Grid:

@(Html.Telerik().Grid<ComplexModels.Models.ProductModel>()
    .Name("Products")
    .Localizable("es-CL")
    .DataKeys(keys =>
    {
        keys.Add(p => p.IdProduct);
    })
    .ToolBar(commands => {
        commands.SubmitChanges().ButtonType(GridButtonType.BareImage);
    })
    .DataBinding(dataBinding =>
        dataBinding.Ajax()
            .Select("_Retrieve", "Home")
            .Update("_Update", "Home")
    )
    .Columns(columns =>
    {
        columns.Bound(p => p.IdProduct).Title("#").Width(40).ReadOnly(true);
        columns.Bound(p => p.Category).ClientTemplate("<#= Category.Code #>").Width(100);
        columns.Bound(p => p.Code).Width(100);
        columns.Bound(p => p.Description);
        columns.Command(commands => commands.Delete().ButtonType(GridButtonType.BareImage)).Width(50);
    })
    .ClientEvents(events => events.OnDataBinding("Products_onDataBinding").OnError("Products_onError"))
    .Editable(editing => editing.Mode(GridEditMode.InCell))
    .Pageable()
    .Sortable()
)

Finally, the controller who does the real job:

[HttpPost]
[GridAction]
public ActionResult _Update([Bind(Prefix = "updated")]List<ProductModel> updatedProducts)
{
    if (updatedProducts != null)
    {
        foreach (var product in updatedProducts)
        {
            var target = DataGateway.Products().Where(p => p.IdProduct == product.IdProduct).FirstOrDefault();
            if (target != null)
            {
                DataGateway.Update(product);
            }
        }
    }
     
    return View(new GridModel(DataGateway.Products()));
}

Problem is, biding doesn't work automatically as i believe at first. Category property is null after binding mechanism ocurrs.

The example shown at http://demos.telerik.com/aspnet-mvc/grid/editingbatch usesa similar Product model, but this class does not consider any complex property, just primitive types, except for the LastSupply property (a DateTime).

Finally, is this scenario really posible at this moment?:

  1. Using complex nested models (at any level)
  2. Using Batch Editing Mode

If YES is the answer, how it must be implemented (in code i mean) binding?

Greetings all.

P.S.: Added captured image displaying the grid just after new values in three DropDownList have been selected: values missing!?
Tags
Grid
Asked by
dejavu
Top achievements
Rank 2
Share this question
or