通过Kendo网格将视图模型中的数据传递给控制器 [英] Passing data in view model to controller via kendo grid

查看:99
本文介绍了通过Kendo网格将视图模型中的数据传递给控制器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

到目前为止,我只是试图实例化我的kendogrid,并通过我的视图模型传递值.我从Telerik的vb.net文档中获得了以下代码.事实是,从.Grid->引发了异常 无法推断出公共可重写重载函数grid(属于T的类)作为gridbuilder(属于t的类型)"

So far, I'm just trying to instantiate my kendogrid and passing through values from my view model. I got the following piece of code from Telerik's documentation for vb.net. The thing is, an exception is thrown from .Grid -> "Type parameter for public overridable overloads function grid(of T as a class) as a gridbuilder(of t) cannot be inferred"

Html.Kendo().Grid().Name("kendogrid")

Html.Kendo().Grid().Name("kendogrid")

我不确定这个错误是什么意思,我也不知道如何解决它.

I'm not sure what this error means and I don't know how to go about fixing it.

查看

$(document).ready(function () {
    var dataSource = new kendo.data.DataSource({
        transport: {
            read: {
                url: "TestAjax",
                dataType: "json",
                type: "GET",
            },
            update: {
                url: "update",
                dataType: "json",
                type: "POST"
            },
            create: {
                url: "CreateInvoiceRecord",
                dataType: "json",
                type: "GET",
            },
            parameterMap: function (options, operation) {
                console.log(operation);
                console.log(options);
                if (operation !== "read" && options.models) {
                    return { models: kendo.stringify(options.models) };
                }
            }
        },
        batch: true,
        pageSize: 20,
        schema: {
            model: {
                id: "itemID",
                fields: {
                    ItemName: { type: "string" },
                    Amount: { type: "number", editable: false, validation: { required: true } },
                    ProductLine: { type: "string" },
                    Status: { type: "string" },
                }
            }
        },
        aggregate: [{ field: "Amount", aggregate: "sum" }
        ]
    });
    $("#kendogrid").kendoGrid({
        DataSource: dataSource,
        pageable:  true,
        height: 550,
        toolbar: ["create", "save"],
        columns: [
            { field: "ItemName", title: "Item", width: "150px" },
            { field: "Amount", title: "Amount", format: "{0:c}", width: "100px", aggregates: ["sum"], footerTemplate: "Total Amount: #=sum#" },
            { field: "ProductLine", title: "Product Line", width: "150px", editor: productLineDropDownEditor},
            { field:  "Status", title: "Status", width: "150px", editor: statusDropDownEditor },
            { command:  "Update", title: "Update" , width:"150px"}],
        editable: true
        });
});

型号

Public Class MyViewModel
    Public Property id As String
    Public Property id2 As String
End Class

推荐答案

最后,我得到了我想要使用hack进行创建,然后使用ajax进行读取的工作.不太确定为什么这样做会起作用,所以我需要进一步研究.我需要将参数传递给连接到kendogrid的控制器-特别是读取和创建操作.我创建了一个视图模型来存储从索引控制器获取的值,然后使用该视图模型将这些值从kendogrid传递到读取和创建操作控制器.由于某种原因,我只能使用ajax将参数传递给读取操作.

Finally got what I wanted working using a hack for create and then ajax for read. Not totally sure why this works the way it does so I'll need to look into it some more. I needed to pass parameters to controllers that are connected to kendogrid - specifically the read and create operations. I created a view model to store the values that I obtained from my Index controller and then used the view model to pass the values from kendogrid to the read and create operation controllers. For some reason, I was only able to pass parameters to the read operation using ajax.

注意:仍然不是最佳解决方案.它两次调用readData控制器,但我不希望这样.

Note: Still not the best solution. It calls readData controller twice and I don't want that.

JavaScript

Javascript

<Script>
    $(document).ready(function () {
        var dataSource = new kendo.data.DataSource({
            transport: {
                read: {    
                    url: "readData",
                    dataType: "json",
                    type: "GET",                  
                },
                create: {
                    url: "CreateInvoiceRecord?trxid=@Model.id2&bcid=@Model.id",
                    dataType: "json",
                    type: "GET",
                },
                parameterMap: function (options, operation) {
                    if (operation !== "read" && options.models) {
                        return { models: kendo.stringify(options.models) };
                    }
                }
            },
            batch: true,
            pageSize: 20,
            schema: {
                model: {
                    id: "itemID",
                    fields: {
                        ItemName: { type: "string" },
                        Amount: { type: "number", validation: { required: true } },
                        ProductLine: { type: "string" },
                        Status: { type: "string" }
                    }
                }
            },
            aggregate: [{ field: "Amount", aggregate: "sum" }]
        });
        $("#kendogrid").kendoGrid({
            dataSource: dataSource,
            navigatable: true,
            pageable:  true,
            height: 550,
            toolbar: ["create", "save"],
            columns: [
                { field: "ItemName", title: "Item", width: "150px" },
                { field: "Amount", title: "Amount", format: "{0:c}", width: "100px", aggregates: ["sum"], footerTemplate: "Total Amount: #=sum#" },
                { field: "ProductLine", title: "Product Line", width: "150px", editor: productLineDropDownEditor},
                { field:  "Status", title: "Status", width: "150px", editor: statusDropDownEditor },
                { command:  "Update", title: "Update" , width:"150px"}],
            editable: true
        });
    });
    function productLineDropDownEditor(container, options) {
            $('<input required name="' + options.field + '"/>')
            .appendTo(container)
            .kendoDropDownList({              
                autoBind: false,
                valuePrimitive: true,
                dataTextField: "name",
                dataValueField: "name",
                dataSource: {
                    transport: {
                        read: {
                            url: "/Customs/getProdLines",
                            dataType: "json"
                        }
                    },
                    schema: {
                        data: "Data",
                        model: {
                            fields: {}
                        }
                    },
                }
            });
    }
    function statusDropDownEditor(container, options) {
            var data = [
                { text: "Active", value: "1" },
                { text: "Paid", value: "2" },
                { text: "Cancelled", value: "3" }
            ]
            $('<input required name="' + options.field + '"/>')
            .appendTo(container)
            .kendoDropDownList({
                valuePrimitive: true,
                dataTextField: "text",
                dataValueField: "value",
                autobind: false,
                dataSource: data
            });
    }
    //function testAjax() {     
    //}
    //data: { 'name': ItemName, 'amount': Amount, 'prodline': ProductLine, 'status': Status },
    $.ajax({
        type: "Get",
        data: { id: "@Model.id", id2:"@Model.id2" },
        url: "readData/",
        dataType: "json",
        success: function (itemList) {
            console.log(itemList);
        }
    });
</script>

型号

Public Class MyViewModel
    Public Property id As String
    Public Property id2 As String
End Class

这篇关于通过Kendo网格将视图模型中的数据传递给控制器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆