如何将更新的数据从网格传递到控制器

时间:2017-07-10 08:25:41

标签: javascript model-view-controller kendo-ui kendo-grid

在我的kendo网格中,在通过弹出编辑器编辑后,更新按钮不起作用,"结果"是一个Ajax呼叫响应,因为我不使用服务我不需要"阅读"部分是我评论它的原因,

数据源初始化:

dataSource = new kendo.data.DataSource({
    transport: {
        //read: {
            //    url: result,
            //    dataType: "json"
        //},
        update: {
            url: "/AdminTool/update_grid",
            dataType: "json"
        },                             
        parameterMap: function (options, operation) {
            if (operation !== "read" && options.models) {
                return { models: kendo.stringify(options.models) };
            }
        }
    },
    batch: true,
    pageSize: 20,
    schema: {
        model: {
            id: "DeviceIP",
            fields: {
                DeviceIP: { editable: false, nullable: true },
                Producer: { type: "string" },
                Model: { type: "string" },
                DeviceType: { type: "string" },
                Description: { type: "string" },
                Username: { type: "string" },
                Password: { type: "string" },
                PublicIP: { type: "string" },
            }
        }
    }
});

Kendo Grid Initialization:

$("#turbingrid").kendoGrid({
    dataSource: result,
    scrollable: false,
    columns: [
       { field: 'DeviceIP', title: 'DeviceIP', width: '100px', id: 'DeviceIP' },
       { field: 'Producer', title: 'Producer', width: '80px', editor: ProductNameDropDownEditor, },
       { field: 'Model', title: 'Model', width: '120px' },
       { field: 'DeviceType', title: 'DeviceType', width: '100px', editor:deviceTypesList },
       { field: 'Description', title: 'Description', width: '100px' },
       { field: 'Username', title: 'Username',width:'120px' },
       { field: 'Password', title: 'Password', width: '100px' },                                          
       { field: 'PublicIP', title: 'PublicIP', width: '120px' },
       { command: ["edit"], title: " ", width: "100px" }],
       editable: "popup",
       edit: function() {
               document.getElementsByName("DeviceIP")[0].disabled = true;
       },                          
       editable: "popup"
});

专栏编辑:

function ProductNameDropDownEditor(container, options) {             
    $('<input  name="Producer" data-type="string"\">')
       .appendTo(container)
       .kendoDropDownList({
           valuePrimitive: true,
           dataSource: mydata,
           dataTextField: "Text",
           dataValueField: "Text",
    });                                           
}              

function deviceTypesList(container, options) {
    $('<input  name="DeviceType" data-type="string" \">')
        .appendTo(container)
        .kendoDropDownList({
            dataSource: mydata_deviceType,
            dataTextField: "Text",
            dataValueField: "Text",
            //dataValueField: "ProductName",
    });
}

我的控制器:

 [HttpPost]
    public ActionResult update_grid(TurbineDvce frm)
    {
        try
        {
            // TODO: Add update logic here

            return RedirectToAction("Index");
        }
        catch
        {
            return View();
        }
    }

我要传递的模型

 public class TurbineDvce
{
    public string TurbineId { get; set; }
    public string DeviceIP { get; set; }
    public string Producer { get; set; }
    public string Model { get; set; }
    public string DeviceType { get; set; }
    public string Comments { get; set; }
    public string Description { get; set; }
    public string Username { get; set; }
    public string Password { get; set; }

    public string PublicIP { get; set; }

}

1 个答案:

答案 0 :(得分:0)

使用parameterMap功能指定在编辑每个Kendo Grid行时,您希望将哪些数据发送到控制器功能。

您还可以在函数中指定基于操作类型返回数据的不同方法。

在您的情况下,例如:

transport: {
    update: {
        url:"/AdminTool/update_grid",
        dataType: "json"
    },
    parameterMap: function (data, operation) {
        if(operation !== "read" && data) {
            return kendo.stringify(data);
        }
    }
}