模型绑定问题与剑道复杂的子对象的属性 [英] Model binding issues with Kendo objects with complex child properties

查看:173
本文介绍了模型绑定问题与剑道复杂的子对象的属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个剑道UI格,让我张贴到服务器的多个变化。绑定到网格模型包含的复杂类型的列表。这里是(简化):

I have a Kendo UI grid which allows me to post multiple changes to the server. The model that is bound to the grid contains a list of a complex type. Here it is (simplified):

public class User
{    
    public int ID { get; set; }
    public string Name { get; set; }
    public List<Role> Roles { get; set; }        
}

要更新我有我的控制器下面签名的方法在服务器上进行更改:

To update the changes on the server I have a method with the following signature in my controller:

public ActionResult UpdateUtilisateurs([DataSourceRequest] DataSourceRequest request, [Bind(Prefix = "models")]IEnumerable<User> users)

用户收集得到正确填写,但是角色列表是空的。我已经使用Firebug的数据实际上是连载来回保证。下面是帖子的时候我刚开到控制器之前更新1行:

The users collection gets filled correctly, however the Roles list is empty. I have ensured using Firebug that the data was actually serialized back and forth. Here's the POST when I update 1 row just before getting to the controller:

filter  
group   
models[0].ID                     16
models[0].Name                   Amir Majic
models[0].Roles[0][Code]         dbadmin
models[0].Roles[0][Description]  Database Administrator
models[0].Roles[0][ID]           33
sort

因此​​,数据看起来不错(可能除了在缺少点角色属性?)。所以,我必须改变方法的签名?我必须创建一个自定义模型粘合剂(虽然我想这是一个相当常见的情况)?

So the data seems OK (except maybe for the missing dot in the Rolesproperty?). So must I change method signature? Do I have to create a custom model binder (though I guess this is a fairly common scenario)?

推荐答案

有完全相同的问题。问题是与子属性的括号(模型[0] .Roles [0] [code] 而不是模型[0 ] .Roles [0] code )。您需要将数据发送到服务器之前解析函数(或更新默认的模型粘合剂)。

Had the exact same problem. The problem is with the brackets of the child properties (models[0].Roles[0][Code] instead of models[0].Roles[0].Code). You will need a parse function before sending the data to the server (or update the default model binder).

剑道的支持给我发了一个解决方案:

Kendo support sent me a solution:

在阿贾克斯的DataSource:

In the Ajax DataSource:

.Update(update => update.Action("Update", "Controller").Data("serialize"))
.Create(create => create.Action("Create", "Controller").Data("serialize"))

后来在视图(或一个JS文件)

Later in the view (or a JS file)

<script>
    function serialize(data) {
        for (var property in data) {
            if ($.isArray(data[property])) {
                serializeArray(property, data[property], data);
            }
        }
    };

    function serializeArray(prefix, array, result) {
        for (var i = 0; i < array.length; i++) {
            for (var property in array[i]) {
                result[prefix + "[" + i + "]." + property] = array[i][property];
            }
        }
    }
</script>

如果你的计划是使用网格编辑复杂对象的该集合,我会告诉你,现在,你会后悔你的决定。只是一个善意的警告,以节省您的时间浪费了几天:)

If your plans are to use a grid to edit that collection of complex objects, I'll tell you right now, you will regret your decision. Just a friendly warning to save you a few days of wasted time :)

这篇关于模型绑定问题与剑道复杂的子对象的属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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