【问题标题】:How to pass updated data from grid to controller如何将更新的数据从网格传递到控制器
【发布时间】:2017-12-13 21:34:02
【问题描述】:

在我的剑道网格中,当我通过弹出编辑器进行编辑后,更新按钮不起作用。“结果”是 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" },
            }
        }
    }
});

剑道网格初始化:

$("#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; }

}

【问题讨论】:

  • 一些注意事项,如果您想在应用更改时自动调用服务器,您可能需要查看autoSync。或者,您可以手动拨打sync。您可以选择使用提供的transport.update 方法或使用AJAX in the update function
  • 您可以指定data 与请求一起发送。或者可以选择使用parameter.map
  • @Sandman 谢谢,但老实说我不知道​​我应该如何实现第二个,我不知道我应该单独编写还是放在我的剑道网格代码之间,
  • 您选择的解决方案可能取决于所需的更新功能行为。逐行更新,即在每次编辑行后调用update_grid,还是在保存所有更新的行时调用单个更新?根据您的偏好更新您的问题,以及您的控制器方法 (update_grid) 以及您认为需要提供更多上下文的任何其他代码。
  • @Sandman 我的每一行都是单个更新,但我应该如何让更新按钮工作?我应该为“编辑:”编写代码?

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


【解决方案1】:

使用parameterMap 函数指定在编辑每个单独的 Kendo Grid 行时要发送到控制器函数的数据。

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

在您的情况下,示例如下:

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

【讨论】:

  • 在剑道示例中有一个部分 url:crudServiceBaseUrl + "/Products",我知道 crudServiceBaseUrl(我的控制器的 url)但是第二部分(/Product)是什么,我的第二个问题是什么读: {...... ?为什么要“阅读”?
  • 我已将上面示例中的 URL 属性替换为使用您在 OP 中的 URL。 read 是用于GET 来自控制器的 Kendo Grid 控件数据的函数。关于transport 配置的完整文档可以在here 找到。
  • 当你说它“不起作用”时,你是什么意思?您更新了问题,但没有提供有关当前/预期行为的详细信息。执行到parameterMap函数,说明控制器函数可能有问题?此外,dataparameterMap 函数中包含什么?您的控制器功能是什么?使用控制器函数 (update_grid) 及其预期的参数结构更新您的问题。
  • 控制器希望从我的视图中获取模型“TurbineDvce”这是我用来绑定我的网格的模型,我还在我的控制器(update_grid)中设置了一个断点,但从未达到点击更新按钮后点,在parameterMap中的“数据”是网格的编辑数据
  • 因此我们可以确定问题出在控制器函数或传递给控制器​​函数的数据结构上。使用update_grid 控制器函数、TurbineDvce 模型的结构以及从parameterMap 函数传递data 的示例更新您的问题
【解决方案2】:

您的控制器操作方法需要参数名称“frm”。所以它会像

parameterMap: function(options, operations){
  if(operation !== "read" && options{
    return {frm: options}
  }
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-06-23
    • 1970-01-01
    • 1970-01-01
    • 2011-10-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多