Create - What is the expected server response?

3 posts, 0 answers
  1. Nick
    Nick avatar
    86 posts
    Member since:
    Mar 2008

    Posted 08 May 2012 Link to this post

    I have a KendoUI grid which is getting data from an ASP.NET MVC application.  The grid populates itself fine but I'm having problems with the create function.

    The data is correctly sent to the controller action and the object persisted to the database but I'm unsure as to what I'm supposed to return.  Currently I'm returning the object but I'm getting a Javascript error on the client "Uncaught TypeError: Cannot read property 'length' of undefined".  The result is the new entry in the grid remains with a "null" Id value and the Name field has a red triangle on the top-left corner.

    Controller Action:
    [HttpPost]
    public JsonResult Create(Department dept)
    {
      _session.Store(dept);
      _session.SaveChanges();
      return Json(dept);
    }

    Grid configuration:
    $(document).ready(function () {
      $("#grid").kendoGrid({
        dataSource: {
          type: "json",
          serverPaging: true,
          pageSize: 10,
          batch: false,
          transport: {
            read: {
              url: "Departments/GetAll",
              dataType: "json"
            },
            create: {
              url: "Departments/Create",
              type: "post",
              dataType: "json"
            }
          },
          schema: {
            data: "Departments",
            total: "TotalCount",
            model: {
              id: "Id",
              fields: {
                Id: {
                  type: "number",
                  editable: false,
                  nullable: true
                },
                Name: {
                  editable: true,
                  nullable: false,
                  validation: {
                    required: true
                  }
                }
              }
            }
          }
        },
        height: 400,
        toolbar: ["create"],
        pageable: true,
        columns: [
        {
          field: "Id",
          title: "Dept Id"
        },
        {
          field: "Name",
          title: "Name"
        }
        ],
        editable: {
          mode: "popup",
          update: false,
          destroy: false
        }
      });
    });


  2. Alexander Valchev
    Admin
    Alexander Valchev avatar
    2875 posts

    Posted 11 May 2012 Link to this post

    Hello Nick,

    Such problem might appear if there is a difference between the root elements of read and create operations.
    In your schema you have defined data as "Departaments", could you please confirm that the returned object is part of the "Departaments" array?

    Greetings,
    Alexander Valchev
    the Telerik team
    Join us on our journey to create the world's most complete HTML 5 UI Framework - download Kendo UI now!
  3. Kendo UI is VS 2017 Ready
  4. Nick
    Nick avatar
    86 posts
    Member since:
    Mar 2008

    Posted 11 May 2012 Link to this post

    Ah, ok.  The grid is populated from the following controller action:

    public JsonResult GetAll(int skip, int take, int page, int pageSize, string group)
    {
      Raven.Client.Linq.RavenQueryStatistics stats;
      var depts = _session.Query<Department>()
        .Statistics(out stats)
        .OrderBy(d=>d.Name)
        .Skip(skip)
        .Take(take)
        .ToList();
     
      var totalSize = stats.TotalResults;
      return Json(new DepartmentListViewModel() { Departments = depts, TotalCount = totalSize }, JsonRequestBehavior.AllowGet);
    }

    Amending my Create function to this fixes the issue.

    [HttpPost]
    public JsonResult Create(Department dept)
    {
      _session.Store(dept);
      _session.SaveChanges();
      DepartmentListViewModel model = new DepartmentListViewModel() { Departments = new List<Department>() { dept }, TotalCount = 0 };
      return Json(model);
    }
Back to Top