我遇到了一个问题,即在将模型的属性发送到我的控制器以从 Kendo Grid 进行 Update 或 Create 调用时未正确更新它。该模型如下所示:
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"))
)
)
更具体地说,我遇到的问题是在我的控制器操作中:
public async Task<ActionResult> EditNote(ReleaseNotesModel model)
model.ReleaseNoteTranslations
始终包含两个空对象(属性为 null 或 0),即我为该属性设置的默认值。如果我没有设置默认值,那么在弹出编辑器中我将没有任何要编辑此属性的字段。所有其他属性都按预期更新。
让我烦恼的是,如果我使用服务器绑定而不是 AJAX,那么所有数据都会被正确接收。所以我决定检查两种情况下发送的请求标头中的数据:
// 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
现在我首先注意到的是objectName[index].PropertyName
vs的语法objectName[index][PropertyName]
我想知道这是否是我的问题的原因,如果是这样,有没有办法让我直接操纵发送的数据来修复它?这可能是 Kendo Grid 通过 Ajax 绑定发送数据的方式中的错误吗?
无论哪种方式,任何帮助将不胜感激!