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

Modifying Grid EditorTemplate result with the viewmodel

3 Answers 120 Views
General Discussions
This is a migrated thread and some comments may be shown as answers.
Nby
Top achievements
Rank 1
Nby asked on 08 Aug 2013, 04:34 PM
Hi,

What I'm trying to do is relatively close to the Grid Editing custom editor sample. The difference in my case is that the list of categories must not be the same for all products. In fact, depending on the product properties, I want to display only a subset of all categories.
In the EditorTemplate ClientCategory.cshtml a model is defined "@model Kendo.Mvc.Examples.Models.ClientCategoryViewModel" but this model is always null. Is there a way to access the ClientCategoryViewModel in the EditorTemplate partial view ?
Or is there another way to acheive this ?

Thank you,
Regards,


3 Answers, 1 is accepted

Sort by
0
Accepted
Vladimir Iliev
Telerik team
answered on 12 Aug 2013, 01:16 PM
Hi,

 
In current scenario I would suggest to modify the "ClientCategory" editor template and configure the DropDownList to load it's data using Ajax. Then you can define a Data function which to return as additional parameter the current row ID by which you can filter the results on the server:

  • ClientCategory editor template:  
    model Kendo.Mvc.Examples.Models.ClientCategoryViewModel
     
    <script>   
        function additionalData(e) {
            var currentRow = $("#Category").closest("tr");
            var grid = currentRow.closest("[data-role=grid]").data("kendoGrid");
            var dataItem = grid.dataItem(currentRow);
     
            var result = {
                id: dataItem.ProductID
            };
     
            return result;
        }
    </script>
     
     
    @(Html.Kendo().DropDownListFor(m => m)
            .DataValueField("CategoryID")
            .DataTextField("CategoryName")
            .DataSource(d => d.Read(r => r.Action("CategoriesRead", "Grid").Data("additionalData")))
    )
  • Example Categories Read action:  
    public ActionResult CategoriesRead(int id)
    {
        var dataContext = new NorthwindDataContext();
        var categories = dataContext.Categories
                .Select(c => new ClientCategoryViewModel
                {
                    CategoryID = c.CategoryID,
                    CategoryName = c.CategoryName
                })
                //example filtering of the data
                .Where(e => e.CategoryID >= id)
                .OrderBy(e => e.CategoryName).ToList();
     
        return Json(categories, JsonRequestBehavior.AllowGet);
    }
        
Kind 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
Nby
Top achievements
Rank 1
answered on 13 Aug 2013, 09:40 AM
Thank you very much, this is exactly what I needed !

Just for my understanding, can you confirm that the model declaration here is useless and there is no way to access the viewmodel from the EditorTemplate ?

Regards,
0
Vladimir Iliev
Telerik team
answered on 13 Aug 2013, 11:36 AM
Hi,

 
The model declaration in the editor template is not useless as it's used to build correctly the editor with all validation attributes as well as adding the correct name and ID attribute (needed for correct data binding).


Kind Regards,
Vladimir Iliev
Telerik
Join us on our journey to create the world's most complete HTML 5 UI Framework - download Kendo UI now!
Tags
General Discussions
Asked by
Nby
Top achievements
Rank 1
Answers by
Vladimir Iliev
Telerik team
Nby
Top achievements
Rank 1
Share this question
or