剑道网格模型与一个IEnumerable属性不更新正确使用AJAX结合时,创建/更新后 [英] Kendo Grid model with an IEnumerable property not updating correctly after Create/Update when using AJAX binding

查看:189
本文介绍了剑道网格模型与一个IEnumerable属性不更新正确使用AJAX结合时,创建/更新后的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在哪里我的模型的属性没有被正确发送到我的控制器时更新或创建一个从剑道电网调用更新的问题。该模型是这样的:

I'm having a problem where a property of my model is not being correctly updated when sending it to my controller for an Update or Create call from a Kendo Grid. The model looks like this:

public class ReleaseNotesModel
{
    public int NoteID { get; set; }
    public int ReleaseID { get; set; }
    public List<TranslationModel> ReleaseNoteTranslations { get; set; }
    public ReleaseNoteType ItemType { get; set; }
}
public class TranslationModel
{
    public int TranslationID { get; set; }
    public string Translation { get; set; }
    public int LanguageID { get; set; }
    public int ItemID { get; set; }
}

下面是在我看来电网:

@(Html.Kendo().Grid<ReleaseNotesModel>()
    .Name("Grid")
    .Columns(columns =>
    {
        columns.Bound(m => m.ItemType).Width(140);
        columns.Bound(m => m.Description);
        columns.Command(command =>
            {
                command.Edit();
                command.Destroy();
            }).Width(170);
    })
    .ToolBar(toolbar => toolbar.Create())
    .Editable(editable => editable
        .Mode(GridEditMode.PopUp)
        .TemplateName("ReleaseNoteTemplate")
        .Window(w => w.Width(620))
        .DisplayDeleteConfirmation(true)
    )
    .Pageable()
    .Sortable()
    .Scrollable()
    .Filterable()
    .DataSource(dataSource => dataSource
        .Ajax()
        .ServerOperation(false)
        //.Server()
        .Events(e => e.Error("grid_error"))
        .Model(model =>
        {
            model.Id(m => m.NoteID);
            model.Field(m => m.ReleaseID).DefaultValue(Model.ReleaseID);
            model.Field(m => m.ItemType).DefaultValue(ReleaseNoteType.NewFeature);
            //defaultTranslationsList is a List<TranslationModel> with two empty objects in it
            model.Field(m => m.ReleaseNoteTranslations).DefaultValue(defaultTranslationsList);
        })
        .PageSize(5)
        .Read(read => read.Action("GetNotes", "ReleaseNotes", new { releaseID = Model.ReleaseID }))
        .Create(create => create.Action("AddNote", "ReleaseNotes"))
        .Update(update => update.Action("EditNote", "ReleaseNotes"))
        .Destroy(destroy => destroy.Action("DeleteNote", "ReleaseNotes"))
    )
)

所以更确切地说,我遇到的问题是,在我的控制器操作:

So more specifically, the problem I am having is that in my controller action:

public async Task<ActionResult> EditNote(ReleaseNotesModel model)

model.ReleaseNoteTranslations 总是包含两个空对象(属性为null或0),即我为此属性设置的默认值。如果我没有设置默认值,那么我不会有任何字段编辑此属性,在弹出的编辑器。如预期所有其他属性都进行更新。

model.ReleaseNoteTranslations always contains two empty objects (properties are null or 0), i.e. the default value which I set for this property. If I set no default value, then I won't have any fields to edit for this property in the popup editor. All the other properties are updated as expected.

什么错误我是,如果我使用的服务器绑定,而不是AJAX,那么所有的数据被正确接收。所以我决定在请求头,检查出的数据在两种情况下被发送:

What bugs me is that if I use server binding instead of AJAX, then all the data is correctly received. So I decided to check out the data in the request headers being sent in both cases:

// Using server binding
ReleaseID:300
NoteID:886
ItemType:1
ReleaseNoteTranslations[0].ItemID:886
ReleaseNoteTranslations[0].LanguageID:1
ReleaseNoteTranslations[0].TranslationID:869
ReleaseNoteTranslations[0].Translation:The module is now released!
ReleaseNoteTranslations[1].ItemID:886
ReleaseNoteTranslations[1].LanguageID:2
ReleaseNoteTranslations[1].TranslationID:870
ReleaseNoteTranslations[1].Translation:Le module est maintenant disponible!
NoteID:886

// Using AJAX binding
sort:
group:
filter:
NoteID:886
ReleaseID:300
ReleaseNoteTranslations[0][TranslationID]:869
ReleaseNoteTranslations[0][Translation]:The module is now released!
ReleaseNoteTranslations[0][LanguageID]:1
ReleaseNoteTranslations[0][ItemID]:886
ReleaseNoteTranslations[1][TranslationID]:870
ReleaseNoteTranslations[1][Translation]:Le module est maintenant disponible!
ReleaseNoteTranslations[1][LanguageID]:2
ReleaseNoteTranslations[1][ItemID]:886
ItemType:1

现在我先在这里注意到的是对象名[索引] .PropertyName的语法 VS 对象名[索引] [属性名]

Now what I notice first here is the syntax of objectName[index].PropertyName vs objectName[index][PropertyName]

我不知道这可能是我的问题的原因,如果是这样,有没有为我去直接操作发送的数据修复它的方法吗?难道这是在路上的一个错误剑道电网通过Ajax的发送数据绑定?

I wonder if this could be the cause of my problem, and if so, is there a way for me to go and directly manipulate the data being sent to fix it? Could this be a bug in the way Kendo Grid sends data through Ajax binding?

无论哪种方式,任何帮助将是非常美联社preciated!

Either way, any help would be much appreciated!

推荐答案

因此​​,在任何情况下,在未来这一绊倒,我接触Telerik的支持,谁给我解释的:

So In case anyone stumbles on this in the future, I contacted Telerik support, who explained to me that:

数据源仅支持值类型,不会序列化
  在格式阵列由该模型粘合剂预期

The dataSource supports only value types and will not serialize the arrays in the format that is expected by the model binder.

他们还使用请求数据函数来调用JavaScript函数将数据转换成正确的格式为我提供了一个解决办法。

They also provided me with a workaround using the request Data function to call a JavaScript function which converts the data into the correct format.

在视图,通过指定调用JavaScript函数的名称修改请求的功能:

In the view, modify the request functions by specifying the name of the JavaScript function to call:

.Create(create => create.Action("AddNote", "ReleaseNotes").Data("serialize"))

然后添加在其中会进行转换的功能:

And then add in the functions which will do the conversion:

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];
        }
    }
}

这篇关于剑道网格模型与一个IEnumerable属性不更新正确使用AJAX结合时,创建/更新后的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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