Skip to main content

EntityDataSetController with Kendo UI Grid create record issue

When binding Kendo UI Grid to odata data source implemented by .NET EntityDataSetController, I find out the creating function did not work well: The creation of record is successfully at the back end but the Kendo grid does not update the record id returned from creation call.

Here is the code for CreateEntity() function:
        [HttpPost]
        protected override SupplierEntity CreateEntity(SupplierEntity se)
        {
            Supplier supplier = new Supplier()
            {
                SupplierCode = se.SupplierCode,
                SupplierName = se.SupplierName,
                SupplierAddress = se.SupplierAddress,
                SupplierCountry = se.SupplierCountry,
                ModifiedOn = DateTime.Now
            };
            db.Suppliers.AddObject(supplier);
            db.SaveChanges();

            se.SupplierID = supplier.SupplierID;
            return se;
        }


Here is the data source definition in front end:
        var supplierModel = {
            id: "SupplierID",
            fields: {
                SupplierID: {
                    editable: false,
                    type: "number",
                    nullable: false
                },
                SupplierCode: {
                    type: "string",
                    validation: {
                        required: true
                    },
                    defaultValue: ""
                },
                SupplierName: {
                    type: "string",
                    defaultValue: ""
                },
                SupplierAddress: {
                    type: "string",
                    defaultValue: ""
                },
                SupplierCountry: {
                    type: "number",
                    validation: {
                        required: true
                    }
                }
            }
        };
        var gridDataSource = new kendo.data.DataSource({
            type: "odata",
            schema: {
                model: supplierModel,
                data: function (data) {
                    return data["value"];
                },
                total: function (data) {
                    return data['odata.count'];
                }
            },
            serverFiltering: true,
            serverPaging: true,
            serverSorting: true,
            pageSize: 50,
            transport: {
                read: {
                    url: "/odata/Suppliers",
                    dataType: "json"
                },
                create: {
                    url: "/odata/Suppliers",
                    type: "POST",
                    dataType: "json"
                },
                update: {
                    url: function (data) {
                        return "/odata/Suppliers(" + data.SupplierID + ")";
                    },
                    type: "PUT",
                    dataType: "json"
                },
                destroy: {
                    url: function (data) {
                        return "/odata/Suppliers(" + data.SupplierID + ")";
                    },
                    type: "DELETE",
                    dataType: "json"
                }
            }
        });



The issue is the data source schema data definition (the text above in red). According the documentation about data source in Kendo UI: The data is the array of data items which the data source contains. The data source will wrap those items as kendo.data.ObservableObject or kendo.data.Model (if schema.model is set). When I checked the return from CreateEntity method, it just returned a single entity. In order to convert the single entity to array of entity, I changed the data definition (the text above in red) into following:
                data: function (data) {
                    if (!data["value"]) {
                        data["value"] = [{}];
                        for (var field in this.model.fields)
                            data["value"][0][field] = data[field];
                    }
                    return data["value"];
                },

After that, the creation function of Kendo grid works as expected.

Comments

Popular posts from this blog

Manage IIS 7 remotely using PowerShell and AppCmd

We can use  Windows PowerShell remoting features  to manage IIS 7 websites remotely.  Currently, remoting is supported on Windows Vista with Service Pack 1 or later, Windows 7, Windows Server 2008, and Windows Server 2008 Release 2.  Start Windows PowerShell as an administrator by right-clicking the Windows PowerShell shortcut and selecting Run As Administrator .  Enable PowerShell Remoting with Enable-PSRemoting -Force Starting a Remote Session using:  Enter-PSSession -ComputerName <COMPUTER> -Credential <USER> Now the PowerShell connected to the remote server. Any commands issued with work against the remote server. We can use the Appcmd.exe command line tool to manage remote server just as what we do locally. For example, to add an application pool: c:\windows\system32\inetsrv\appcmd add apppool /name:"Contoso" /managedPipelineMode:Integrated /managedRuntimeVersion:"v4.0" /enable32BitAppOnWin64:true To change application pool for a

Entity framework code first error: OriginalValues cannot be used for entities in the Added state

When I was using Entity framework code first, I encountered an error when I tried to create an entity into database. The entity is: [ Table (" EmployeeProfile ")]     public partial class EmployeeProfile     {         [ Key ]         [DatabaseGeneratedAttribute(DatabaseGeneratedOption.Identity)]         public int EmployeeProfileID { get; set; }         [ ForeignKey ("Employee")]         public int EmployeeID { get; set; }         public virtual Employee Employee { get; set; }         [ ForeignKey (" Profile ")]         public int ProfileID { get; set; }         public virtual Profile Profile { get; set; }         [ Required ]         [ StringLength (255)]         public string ProfileValue { get; set; }     } When creating the entity, some entities have the ProfileValue="", this causes the EntityValidationException with the detailed message " OriginalValues cannot be used for entities in the Added state ". I want to allow the Prof

X509Certificate2: The system cannot find the file specified.

When I use the new X509Certificate2(fileName, password, X509KeyStorageFlags.DefaultKeySet) to create certificate from certificate file containing private key in my web application, I got following error message: System . Security . Cryptography . CryptographicException : The system cannot find the file specified . at System . Security . Cryptography . CryptographicException . ThrowCryptogaphicException ( Int32 hr ) at System . Security . Cryptography . X509Certificates . X509Utils . _LoadCertFromBlob ( Byte [] rawData , IntPtr password , UInt32 dwFlags , Boolean persistKeySet , SafeCertContextHandle & pCertCtx ) at System . Security . Cryptography . X509Certificates . X509Certificate . LoadCertificateFromBlob ( Byte [] rawData , Object password , X509KeyStorageFlags keyStorageFlags ) at System . Security . Cryptography . X509Certificates . X509Certificate2 .. ctor ( Byte [] rawData , String password , X509KeyStorageFlags keyStorageFlags ) In orde