【问题标题】:Kendo UI Dropdown Column value is not submitted to controllerKendo UI Dropdown Column 值未提交给控制器
【发布时间】:2015-10-07 12:16:01
【问题描述】:

我正在使用 ASP.NET MVC 为 Web 实现 Kendo Ui。下面是 Kendo UI Grid 的代码。

 <div id="grid"></div>

<script>
    $(function () {
        $("#grid").kendoGrid({
            height: 400,
            columns: [
                "Name",
                { field: "Address" },
                { field: "Latitude", width: "150px" },
                { field: "Longitude", width: "150px" },
                { field: "UserId", editor: UserDropDownEditor, title: "User", template: "#=UserName#" },
                { command: "destroy", title: "Delete", width: "110px" }
            ],
            editable: true, // enable editing
            pageable: true,
            sortable: true,
            filterable: true,
            toolbar: ["create", "save", "cancel"], // specify toolbar commands
            dataSource: {
                serverPaging: true,
                serverFiltering: true,
                serverSorting: true,
                pageSize: 10,
                schema: {
                    data: "Data",
                    total: "Total",
                    model: { // define the model of the data source. Required for validation and property types.
                        id: "Id",
                        fields: {
                            Id: { editable: false, nullable: true },
                            Name: { validation: { required: true } },
                            Address: { validation: { required: true } },
                            Latitude: { validation: { required: true } },
                            Longitude: { validation: { required: true } },
                            UserId: { validation: { required: true } }
                        }
                    }
                },
                batch: true, // enable batch editing - changes will be saved when the user clicks the "Save changes" button
                transport: {
                    create: {
                        url: "/Admin/Sites/Create", //specify the URL which should create new records. This is the Create method of the HomeController.
                        type: "POST" //use HTTP POST request as the default GET is not allowed for ASMX
                    },
                    read: {
                        url: "/Admin/Sites/Read", //specify the URL which should return the records. This is the Read method of the HomeController.
                        contentType: "application/json",
                        type: "POST" //use HTTP POST request as by default GET is not allowed by ASP.NET MVC
                    },
                    update: {
                        url: "/Admin/Sites/Update", //specify the URL which should update the records. This is the Update method of the HomeController.
                        type: "POST" //use HTTP POST request as by default GET is not allowed by ASP.NET MVC
                    },
                    destroy: {
                        url: "/Admin/Sites/Destroy", //specify the URL which should destroy the records. This is the Destroy method of the HomeController.
                        type: "POST" //use HTTP POST request as by default GET is not allowed by ASP.NET MVC
                    },
                    parameterMap: function (data, operation) {
                        if (operation != "read") {
                            // post the products so the ASP.NET DefaultModelBinder will understand them:

                            // products[0].Name="name"
                            // products[0].ProductID =1
                            // products[1].Name="name"
                            // products[1].ProductID =1

                            var result = {};

                            for (var i = 0; i < data.models.length; i++) {
                                var site = data.models[i];

                                for (var member in site) {
                                    result["sites[" + i + "]." + member] = site[member];
                                }
                            }

                            return result;
                        } else {
                            return JSON.stringify(data)
                        }
                    }
                }
            }
        });

        function UserDropDownEditor(container, options) {
            $('<input required data-text-field="Name" data-value-field="Id" data-bind="value:' + options.field + '"/>')
                .appendTo(container)
                .kendoDropDownList({
                    autoBind: false,
                    dataSource: {
                        transport: {
                            read: "/Admin/Sites/GetUsersList"
                        }
                    }
                });
        }


    });
</script>

这会在网格中正确显示下拉菜单。但是当我通过在下拉列表中选择值来更新任何行时,我没有在控制器中获得选定的值。

[HttpPost]
        public ActionResult Update(IEnumerable<Site> sites)
        {
            try
            {

                //Iterate all updated products which are posted by the Kendo Grid
                foreach (var siteModel in sites)
                {
                    // Create a new Product entity and set its properties from productViewModel
                    var site = db.Sites.Find((int)siteModel.Id);
                    site.Name = siteModel.Name;
                    site.Address = siteModel.Address;
                    site.Latitude = siteModel.Latitude;
                    site.Longitude = siteModel.Longitude;
                    site.UserId = siteModel.UserId;
                    site.ModifiedBy = User.Identity.GetUserId();
                    site.ModifiedOn = DateTime.Now;

                }

                // Save all updated products to the database
                db.SaveChanges();

            }
            catch (DbEntityValidationException e)
            {
                foreach (var eve in e.EntityValidationErrors)
                {
                    Console.WriteLine("Entity of type \"{0}\" in state \"{1}\" has the following validation errors:",
                        eve.Entry.Entity.GetType().Name, eve.Entry.State);
                    foreach (var ve in eve.ValidationErrors)
                    {
                        Console.WriteLine("- Property: \"{0}\", Error: \"{1}\"",
                            ve.PropertyName, ve.ErrorMessage);
                    }
                }
            }
            //Return emtpy result
            return Json(null);
        }

site.UserId = siteModel.UserId;

这始终为空。谁能指出这里出了什么问题。

更新

这表明 id 正在被传递。但是当我尝试在服务器端观看时,它不可用。

【问题讨论】:

    标签: asp.net asp.net-mvc kendo-ui


    【解决方案1】:

    添加数据值原语属性:

    $('<input required data-text-field="Name" data-value-field="Id" data-value-primitive="true" data-bind="value:' + options.field + '"/>')
    

    https://medium.com/falafel-software/kendo-ui-mvvm-and-the-primitive-type-value-7d81b4cc31ce

    【讨论】:

    • 如果您能提供帮助,还有一件事。更新记录后,网格不会刷新。即使在正确更新数据库后,红旗仍保留在网格中。关于如何解决它的任何建议。 ?
    • 我找到了另一种方法。我正在从成功更新的服务器返回数据。而且效果很好。感谢您的所有帮助:)
    • 对,这就是我们为个人更新所做的,但不确定它是否适用于集合。
    【解决方案2】:

    如果你给你的输入一个名字属性会发生什么

    $('<input name="UserId" id="UserId" required data-text-field="Name" data-value-field="Id" data-bind="value:' + options.field + '"/>')
    

    【讨论】:

    • 尝试使用 Fiddler 之类的工具来监控流量并查看帖子中是否包含 UserId?
    • 它将用户 ID 视为一个对象,而不仅仅是一个 guid。你的 Site 有用户属性吗?
    • 我在 mvc 中仅使用 javascript 库没有太多经验,因为我只是使用了 mvc 扩展。如果您查看他们的自定义编辑器演示,他们正在使用 Category 对象回发to 而不仅仅是一个 Id.. 您可能需要将 User 属性添加到您的 Site 类demos.telerik.com/kendo-ui/grid/editing-custom
    猜你喜欢
    • 1970-01-01
    • 2022-12-30
    • 1970-01-01
    • 2016-11-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-01-07
    • 1970-01-01
    相关资源
    最近更新 更多