Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why is the update operation not posting any data?

I am using the Kendo Grid with inline editing. When I click the "Update" button, a POST gets made to my controller method with this signature. The controller action gets hit, so the POST is working.

[HttpPost]
    public HttpResponseMessage SaveAccountAdmin(string jsonCompanyContacts)

However the POST data in the update operation never arrives - its always null.

update: {
              url: "/Company/SaveAccountAdmin",
              contentType: "application/json; charset=utf-8",
              type: "POST",
              dataType: "json",
              data: {
                  jsonCompanyContacts: "John Doe"
              }
          },

Here is the FULL data source code.

var dataSource = new kendo.data.DataSource(
  {
      batch: false,
      pageSize: 10,

      transport: {
          create: {
              url: "/Company/SaveAccountAdmin",
              contentType: "application/json; charset=utf-8",
              type: "POST",
              dataType: "json"
          },

          read: {
              url: "/Company/ReadAccountAdmin"
          },

          update: {
              url: "/Company/SaveAccountAdmin",
              contentType: "application/json; charset=utf-8",
              type: "POST",
              dataType: "json",
              data: {
                  jsonCompanyContacts: "John Doe"
              }
          },
          //destroy: {},

          parameterMap: function (data, type) {
              return kendo.stringify(data);
          }
      },

this doesnt work either:

update: {
              url: "/Company/SaveAccountAdmin",
              contentType: "application/json; charset=utf-8",
              type: "POST",
              dataType: "json",
              //data: { "jsonCompanyContacts": kendo.stringify({ jsonCompanyContacts: "John Doe" }) }

              data: { "jsonCompanyContacts": "John Doe" }
          },
          //destroy: {},

          parameterMap: function (data, type) {
              return kendo.stringify(data);
          }

BUT THIS WORKS- WHY?

update: {
              url: "/Company/SaveAccountAdmin",
              contentType: "application/json; charset=utf-8",
              type: "POST",
              dataType: "json",
              //data: { "jsonCompanyContacts": kendo.stringify({ jsonCompanyContacts: "John Doe" }) }

              //data: { "jsonCompanyContacts": "John Doe" }
          },
          //destroy: {},

          parameterMap: function (data, type) {
              return kendo.stringify({ "jsonCompanyContacts": "John Doe" });
          }
like image 705
Greg Avatar asked Nov 04 '22 05:11

Greg


2 Answers

The value is not passed to the controller as a string. Try using a model. This might help: MVC3 & JSON.stringify() ModelBinding returns null model

UPDATE

You really don't want to do it like that. Might work in theis one case, but you are shooting yourself in the foot.

Model

public class CompanyContactModel
{
    public string CompanyContacts { get; set; }
}

Controller Signature

public JsonResult SaveAccountAdmin(CompanyContactModel companyContactModel)
...

Better

public JsonResult SaveAccountAdmin([DataSourceRequest]DataSourceRequest request, CompanyContactModel companyContactModel)
    ...
    Update and Return and put into List
    If error: ModelState.AddModelError(string.Empty, e.Message);

    DataSourceResult result = [Your Model List].ToDataSourceResult(request, ModelState);
    return Json(result, JsonRequestBehavior.AllowGet);
}
like image 167
Trey Gramann Avatar answered Nov 15 '22 13:11

Trey Gramann


Try doing this in your update definition:

  update: {
      url: "/Company/SaveAccountAdmin",
      contentType: "application/json; charset=utf-8",
      type: "POST",
      dataType: "json",
      data:{ "jsonCompanyContacts":  kendo.stringify({ jsonCompanyContacts: "John Doe"  })}
      }

You might have to remove the operation in your parameterMap for this to work. The main thing is that you want to post a variable with the same name as in your controller. That variable should contain your stringified data.

You could also move this operation to your parameterMap if you want.

like image 23
Logard Avatar answered Nov 15 '22 13:11

Logard