5

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" });
          }
Greg
  • 2,654
  • 3
  • 36
  • 59

3 Answers3

1

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.

Logard
  • 1,494
  • 1
  • 13
  • 27
  • Thanks for the reply Logard. I tried your code leaving in the parameter map (because I think inline editing needs it) and it didn't work. The data is still null. I then commented out the parameter map and the POST doesn't happen. – Greg Feb 08 '13 at 14:39
  • I also tried allowing the parameter map to stringify the data(See my update). Still null.. – Greg Feb 08 '13 at 14:50
  • Thanks Logard - one of your suggestions worked. I just dont know why :-) – Greg Feb 08 '13 at 14:58
1

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);
}
Community
  • 1
  • 1
Trey Gramann
  • 2,004
  • 20
  • 23
  • Hey Trey - can you be more specific please? surely the return kendo.stringify(data) is sending a string (albeit a json string)? – Greg Feb 08 '13 at 14:45
  • MVC controllers can accept JSON models as actual models; you don't have to parse them in as a string. Alter your controller to accept a model that matches the format of the JSON you're sending. – anaximander Feb 08 '13 at 14:52
  • @anaximander - thanks for the reply. What would my model(signature) in the action method look like? Could you give me a basic example with FirstName, LastName. – Greg Feb 08 '13 at 14:57
  • I am close to solving this. Can anyone tell me how to pass the grid data to the parameterMap? model doesnt work. – Greg Feb 08 '13 at 15:13
  • Im not using Kendo MVC so I dont have DataSourceRequest. I dont understand how I can send json formatted data into a CompanyContactModel object.Doesnt it have to be deserialized first? So now my controller signature looks like [HttpPost] public JsonResult EditAccountAdmin(List jsonCompanyContacts). Now the problem is getting the parameterMap to send the grid data. Been searching for hours. Nothing – Greg Feb 08 '13 at 17:51
  • Actually - I'm not close to solving anything. 1 step forward = 10 steps back. The tickets I have raised have a 48 hour SLA. None have been answered yet. This just isn't a viable product to use in a production scenario. Thanks everyone who tried to help. – Greg Feb 09 '13 at 15:25
0

I had similar problem like you.I am getting values from the broswer , but its not posting the values to the update action model.

In my case I used "[ScriptIgnore(ApplyToOverrides = true)]" on the model which works fine.

Cherry
  • 675
  • 3
  • 10
  • 28