2

I know this question has been asked many times, but this is different.

I have kendo grdi which i get selected and not selected type, the user have free role to make changes and that works. I did tried exmples like this Example1 ,Example2 and many others.

Now the problem is my json object that am trying to pass to the controller became null, I this is what i have tried below.

Can you please correct me if there's something wrong am doing ?

Javascript

JsonObj : This is what i get when i deselect from grid checkbox

[ { mailID: '10' , roleID: '5' , isMailSelected: 'false' } , { 
  mailID: '11' , roleID: '5' , isMailSelected: 'false' } , { 
 mailID: '19' , roleID: '9' , isMailSelected: 'false' } ]





function goToControllerSave() {
    var jsonObj = displayFilterResults();
    $.ajax({

        url: '@Url.Action("Save", "Account")',
        type: "POST",
        dataType: "json",
        traditional: true,
        data: { myobj: jsonObj },  
        success: function (data) {

        },
        error: function (data) {

        }
    })

}


$(function () {
    $('#Grid1').on('click', '.chkbx', function () {
        var checked = $(this).is(':checked');
        var grid = $('#Grid1').data().kendoGrid;
        var dataItem = grid.dataItem($(this).closest('tr'));
        dataItem.set('isSelected', checked);
    })
})


//My json obj: this returns results on Alert

function displayFilterResults() {
    var items = "[";
    var dataSource = $("#Grid1").data("kendoGrid").dataSource;   
    var filters = dataSource.filter();
    var allData = dataSource.data();
    var query = new kendo.data.Query(allData);
    var filteredData = query.filter(filters).data;

  items = items + " { mailID: '" + item.mailID + "' , roleID: '" + 
  item.roleID + "' , isSelected: '" + item.isSelected + "' } ,";

    }); 
    if (items.charAt(items.length - 1) == ',') {
        items = items.substr(0, items.length - 1);
    }
    items = items + "]";

    alert(items)
    return items;
}

My conntroller

   public class myItems
    {

        public string mailID { set; get; }
        public string roleID { set; get; }
        public string isSelected { set; get; }
    }

    [HttpPost]
    public ActionResult Save(List<myItems> myobj)
    {
       --------------
       --------------
    }
IT Forward
  • 367
  • 2
  • 7
  • 28

1 Answers1

1

In the examples you mention, json object is an actual object, not the string representation. Then JSON.stringify() is used to get string formatted using double quotation marks, removing spare spaces, etc. Unfortunately MVC application has trouble parsing json that doesn't look like this.

Since you create a string directly, you need to produce similar json format.

var json = '{"myobj":[';
json +='{mailID:"10",roleID:"5",isMailSelected:"false"},';
json+='{mailID:"11",roleID:"5",isMailSelected:"false"},';
json +='{mailID:"19",roleID:"9",isMailSelected:"false"}'
json +=']}';
$.ajax({
    url: '@Url.Action("Save", "Account")',
    type: "POST",
    contentType: "application/json",
    dataType: 'html',
    data: json,
    success: function (data) {

    },
    error: function (data) {
    }
})

In the class I have updated the name of member isSelected, cause I saw it didn't match json and I assume you changed it to isMailSelected.

public class myItems
{
    public string mailID { set; get; }
    public string roleID { set; get; }
    public string isMailSelected { set; get; }
}

In my controller I have the same you posted

[HttpPost]
public ActionResult Save(List<myItems> myobj)
{
    return View();
}
derloopkat
  • 6,232
  • 16
  • 38
  • 45
  • this looks like will work after getting the correct format, it must be like the one. which means names are in qouts. how can i get that ? { "items": [ { "mailID ": + item.mailID +, "roleID": + item.roleID +, "isMailSelected ": item.isSelected }, { "mailID": + item.mailID +, "roleID": + item.roleID +, "isMailSelected ": item.isSelected } ] } – IT Forward Jul 07 '17 at 13:46
  • I believe code would be better if you simply create a json object and push items into the array rather than concatenating strings. – derloopkat Jul 07 '17 at 13:49
  • I don't understand what you trying to say – IT Forward Jul 07 '17 at 13:56
  • In my code it also works if I do var json = { myobj: [] }; json.myobj.push({ mailID: 'billgates@micrsoft.com', roleID: 'President' }); Then in Ajax call: data: JSON.stringify(json) You can populate the array by pushing items into json.myobj. Then stringify will format. – derloopkat Jul 07 '17 at 14:01
  • Then just amend displayFilterResults() for producing formatted json – derloopkat Jul 07 '17 at 14:26
  • var jsonObj = displayFilterResults(); and then on ajax i did this and still returns null data: JSON.stringify(jsonObj), – IT Forward Jul 07 '17 at 14:33
  • because this function is not iterating to generate items, it only try to create one and even this part is probably wrong. The variable item should be set while iterating through items. – derloopkat Jul 07 '17 at 14:36
  • Now am running out of options on what to do as am not getting results – IT Forward Jul 07 '17 at 14:37
  • you have a kendo datasource then iterate through items, see https://telerikhelper.net/2013/02/26/how-to-iterate-all-the-data-in-kendo-ui-data-source/ there is a *for* statement by the end of the page. For each item in the datasource you add an item in you json array. – derloopkat Jul 07 '17 at 14:39
  • If that code isn't clear then create new question in stackoverflow. – derloopkat Jul 07 '17 at 14:45
  • I have formatted my json to the required format and it looks good as i have validated it online. Now the problem is when passing it to the action, count is 0...not sure what am i missing – IT Forward Jul 10 '17 at 11:48