Modifying Grid EditorTemplate result with the viewmodel

4 posts, 1 answers
  1. Nby
    Nby avatar
    5 posts
    Member since:
    Sep 2012

    Posted 08 Aug 2013 Link to this post

    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,


  2. Answer
    Vladimir Iliev
    Admin
    Vladimir Iliev avatar
    2172 posts

    Posted 12 Aug 2013 Link to this post

    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!
  3. UI for ASP.NET MVC is VS 2017 Ready
  4. Nby
    Nby avatar
    5 posts
    Member since:
    Sep 2012

    Posted 13 Aug 2013 Link to this post

    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,
  5. Vladimir Iliev
    Admin
    Vladimir Iliev avatar
    2172 posts

    Posted 13 Aug 2013 Link to this post

    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!
Back to Top
UI for ASP.NET MVC is VS 2017 Ready