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

Create - What is the expected server response?

2 Answers 234 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Nick
Top achievements
Rank 1
Nick asked on 08 May 2012, 10:33 PM
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 Answers, 1 is accepted

Sort by
0
Alexander Valchev
Telerik team
answered on 11 May 2012, 07:25 AM
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!
0
Nick
Top achievements
Rank 1
answered on 11 May 2012, 11:23 AM
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);
}
Tags
Grid
Asked by
Nick
Top achievements
Rank 1
Answers by
Alexander Valchev
Telerik team
Nick
Top achievements
Rank 1
Share this question
or