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

Grid read with parameter by ajax successfully, but update fail in front end

2 Answers 351 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Penny
Top achievements
Rank 1
Penny asked on 13 Apr 2019, 11:31 AM

I'm trying to use kendo grid and ASP.NET Web Api,

I use read (with additional parameter), and update function,

but when I execute update (Save Changes), front end console error happen....

so, I appropriate any help..

 

----------------------------------------------------------------------------------------------------------------

My Step as below:

step1. query with additional parameter with Ajax (choose datepicker, and press "QueryByDate" button) => success 

step2. modify data in Grid

step3. then press "Save Changes" button to update => console error as below:

 

kendo.web.js:7040 Uncaught TypeError: r.transport[i].call is not a function
    at Object.<anonymous> (kendo.web.js:7040)
    at Function.Deferred (jquery.min.js:2)
    at init._promise (kendo.web.js:7037)
    at init._send (kendo.web.js:7059)
    at init.sync (kendo.web.js:6802)
    at init.saveChanges (kendo.web.js:61295)
    at HTMLAnchorElement.<anonymous> (kendo.web.js:61457)
    at HTMLDivElement.dispatch (jquery.min.js:3)
    at HTMLDivElement.r.handle (jquery.min.js:3)

----------------------------------------------------------------------------------------------------------------

1.(front-end) HTML:

<input id="datepicker" title="datepicker" />

<button id="btnSearch">QueryByDate</button>
<div id="grid">

<script>
    $("#btnSearch").click(() => {
        grid.setDataSource(ds);
        grid.dataSource.read();
    });

    let ds = new kendo.data.DataSource({
        transport: {
            read: function (options) {
                let myDate = $("#datepicker").val();
                $.ajax({
                    url: "API_Reg12StationStress/GetByDate",
                    type:"get",
                    dataType: "json",
                    data: { queryDate: myDate},
                    success: function (result) {
                        options.success(result);
                    }
                });
            },
            update: {
                url: "API_Reg12StationStress/Update2",
                type: "get",
                dataType: "json"
            },
            parameterMap: function (options, operation) {
                if (operation !== "read" && options.models) {
                    return { models: kendo.stringify(options.models) };
                }   
            }
        },
        batch: true,
        schema: {
            model: {
                id: "id",
                fields: {
                    id: { editable: false },
                    name_level1: { editable: false },
                    name_level2: { editable: false },
                    stress_data1: { editable: true },
                    stress_data2: { editable: true },
                    data_YYYYMMDD: { editable: false }
                }
            }
        }
    });

    let grid = $("#grid").kendoGrid({
        toolbar: [
            { name: "save" },
            { name: "cancel" }
        ],
        editable: false,
        noRecords: {
            template: "查無資料"
        },
        columns: [
            { field: "name_level1", title: "廠所別" },
            { field: "name_level2", title: "記錄地點" },
            {
                title: "水壓指示",
                columns: [
                    { field: "stress_data1", title: "上午" },
                    { field: "stress_data2", title: "下午16時" },
                    { field: "data_YYYYMMDD", title: "提供時間" }
                ]
            }
        ]
    }).data("kendoGrid");

</script>

 

----------------------------------------------------------------------------------------------------------------

2.ASP.NET MVC (back-end) :

public class API_Reg12StationStressController : Controller

{

     MyEntities _db = new MyEntities();

 

      public ActionResult GetByDate(string queryDate) //Query By Date (ex:2019/4/12)
        {
            DateTime dtQuery = DateTime.Parse(queryDate);
            List<REG12_STATION_STRESS> models = _db.REG12_STATION_STRESS.Where(model => model.report_date == dtQuery).ToList();
            return Json(models, JsonRequestBehavior.AllowGet);
        }


        public ActionResult Update2() //Update batch data
        {
                 var jsonString = this.HttpContext.Request.QueryString.Get("models");

                IEnumerable<REG12_STATION_STRESS> updateItems = null;

                DataContractJsonSerializer deserializer = new DataContractJsonSerializer(typeof(IEnumerable<REG12_STATION_STRESS>));
                using (MemoryStream stream = new MemoryStream(Encoding.UTF8.GetBytes(jsonString)))
                {
                    updateItems = (IEnumerable<REG12_STATION_STRESS>)deserializer.ReadObject(stream);
                }

                foreach (var item in updateItems)
                {
                    var target = _db.REG12_STATION_STRESS.FirstOrDefault(x => x.id == item.id);
                    target.stress_data1 = item.stress_data1;
                    target.stress_data2 = item.stress_data2;
                }
                _db.SaveChanges();

                bool act = true;
                return Json(act, JsonRequestBehavior.AllowGet);
        }

 

 

2 Answers, 1 is accepted

Sort by
0
Alex Hajigeorgieva
Telerik team
answered on 16 Apr 2019, 04:08 PM
Hello, Penny,

It is imperative that all the transport operations are defined in the same way - either all as objects or as functions:

https://docs.telerik.com/kendo-ui/framework/datasource/crud#transport

I suppose that the error might stem from the way the data source operations are defined now.

Could you try making the same and let me know if the error disappears?

Kind Regards,
Alex Hajigeorgieva
Progress Telerik
Get quickly onboarded and successful with your Telerik and/or Kendo UI products with the Virtual Classroom free technical training, available to all active customers. Learn More.
0
Penny
Top achievements
Rank 1
answered on 19 Apr 2019, 05:10 AM
Thanks for your support a lot, Now we solve it !
Tags
Grid
Asked by
Penny
Top achievements
Rank 1
Answers by
Alex Hajigeorgieva
Telerik team
Penny
Top achievements
Rank 1
Share this question
or