sanzy
sanzy

Reputation: 815

how to bind model to kendo grid datasource

i have developed a web application using asp.net mvc4 and kendo ui tools..

in there i need to bind a model to grid datasource or schema directly without creating fields in schema..

here is the code of my grid..

function LoadGridView() {

        dataSource = new kendo.data.DataSource({
            transport: {
                read: {
                    url: "Items/ReadItemDetails",
                    dataType: "json"
                },
                create: {
                    url: "Items/InsertItemDetails",
                    dataType: "json"
                },
                update: {
                    url: "Items/UpdateItemDetails",
                    Type: "POST",
                    dataType: "jsonp"
                },
                destroy: {
                    url: "Items/DeleteItemDetails",
                    dataType: "json"
                }
            },

            batch: true,
            pageSize: 30,
            schema: {
                model: {
                    id: "ItmKy",
                    fields: {
                        ItmCd: { editable: true, nullable: true },
                        ItmNm: { editable: true, nullable: true },
                        Unit: { editable: true, nullable: true },
                        isAct: { editable: true, nullable: true }
                    }
                }
            }
        });
}

i need to pass a model name to schema instead of this lines

schema: {
                    model: {
                        id: "ItmKy",
                        fields: {
                            ItmCd: { editable: true, nullable: true },
                            ItmNm: { editable: true, nullable: true },
                            Unit: { editable: true, nullable: true },
                            isAct: { editable: true, nullable: true }
                        }
                    }
                }

i tried like this but then the grid is not working..

schema: {
                    model: @Model
                }

can somebody please help me and tell that how can i bind a model to schema or to DataSource if possible..

here is the model class

namespace KendoModel
{
    public class ItemMas_LookUp 
    {
        public long ItmKy { get; set; }
        public string ItmCd { get; set; }
        public string ItmNm { get; set; }
        public int UnitKy { get; set; }
        public int isAct { get; set; }
        public string Unit { get; set; }

        public ItemMas_LookUp() 
        {

        }
    }
}

Upvotes: 1

Views: 10099

Answers (1)

Jeff
Jeff

Reputation: 12163

This is how I did it in pure JS, no backend components:

My model:

var PageItem = kendo.data.Model.define({
    id: "Id"
});

My data source (in the same scope!):

var pageItemDs = new kendo.data.DataSource({
    transport: {
        ...
    },
    schema: {
        model: PageItem
    }
});

If you wish to use the ASP.NET components, you will need to define the grid itself in Razor, you wont be able to mix JS and Razor like you are trying to.

Upvotes: 1

Related Questions