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

how to create new record via js ?

1 Answer 61 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Vincent
Top achievements
Rank 1
Iron
Vincent asked on 28 Nov 2019, 01:21 PM

my code as blow:

function GridDataSource(gridName,path, readHandler, createHandler, updateHandler,deleteHandler, model) {

            var grid = GetGrid(gridName);
            var urlRead = '@Url.Content("~")' + encodeURI(path + "?handler=" + readHandler );
            var urlCreate = '@Url.Content("~")' + encodeURI(path + "?handler=" + createHandler);
            var urlUpdate = '@Url.Content("~")' + encodeURI(path + "?handler=" + updateHandler);
            var urlDestroy = '@Url.Content("~")' + encodeURI(path + "?handler=" + deleteHandler);


            if (model == null)
                model = { id: "Id" };

            var dataSource = new kendo.data.DataSource({
                type: "aspnetmvc-ajax",
                pageSize: 1000,
                serverPaging: false,
                serverGrouping: false,
                transport: {
                    read: {
                        url: urlRead,
                        data: kendo.antiForgeryTokens(),
                    },
                    create: {
                        url: urlCreate,
                        data: kendo.antiForgeryTokens(),
                        cache: true
                    },
                    update: {
                        url: urlUpdate,
                        data: kendo.antiForgeryTokens(),
                    },
                    destroy: {
                        url: urlDestroy,
                        data: kendo.antiForgeryTokens(),
                        cache: true
                    },
                    parameterMap: function (data, type) {
                        if (type == "create") {
                            // send the created data items as the "models" service parameter encoded in JSON
                            return { models: kendo.stringify(data.models) };
                        }
                    }
                },
                schema: {
                    data: "Data",
                    total: "Total",
                    errors: "Errors",
                    model: model
                }
            });


            grid.bind("dataBound", function () {

                if (grid.dataSource.data().length>0)
                    grid.select("tr:eq(0)");

            });
            grid.setOptions({
                dataSource: dataSource,
                persistSelection: true,
            });

            //grid.setDataSource(dataSource);

        }

 

function BtnAddClientClick(e) {
        var ds = GetGrid("DgClient").dataSource;
        var newRow = kendo.observable({
            Id: Guid(),
            Clientname: "xxxxx",
            Status: "Draft",
            Comments: ""
        });

        var dataItem = ds.insert(0, newRow);
        ds.sync();
    }

 

public ActionResult OnPostAddClient([DataSourceRequest]DataSourceRequest request, Cmclient client)
        {
            var data = Request.Form;
            var id = data["Id"][0];

            return new JsonResult("OK");

            //ds.Cmclient.Add(client);
            //ds.SaveChanges();
            //return new JsonResult(new[] { client }.ToDataSourceResult(request, ModelState));
        }

 

 

not work. why? please help.

1 Answer, 1 is accepted

Sort by
0
Alex Hajigeorgieva
Telerik team
answered on 03 Dec 2019, 06:52 AM

Hi, Vincent,

Thank you very much for the provided code snippets.

The reason why it does not work is the parameterMap function which is sending a parameter called models which is undefined - data.models is only available for batch operations.

https://docs.telerik.com/kendo-ui/api/javascript/data/datasource/configuration/transport.parametermap

In the case when batch operations are not used, data is the newly created, updated or destroyed model. So the following change should be able to send the model in the expected format:

parameterMap: function (data, type) {
   if (type !== "read") {
         // send the data item as a service parameter encoded in JSON
         return kendo.stringify(data);
    }
}
Give this a try and let me know in case you need further information.

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.
Tags
Grid
Asked by
Vincent
Top achievements
Rank 1
Iron
Answers by
Alex Hajigeorgieva
Telerik team
Share this question
or