我有一个可编辑的Kendo UI Grid,带有自定义编辑器。每当我尝试在自定义编辑器中单击文本框外(这是当Kendo将值设置为网格模型时),我会收到如下错误的错误:
0x800a138f - JavaScript runtime error: Unable to set property 'PercentWeight' of undefined or null reference
在调试器中,异常发生在:
function anonymous(d,value) {
d.PercentInfo.PercentWeight=value
}
当我将鼠标悬停在d
上时,除了IngredientViewModel
外,它的所有PercentInfo
属性都有,当我将鼠标悬停在PercentInfo
上时,它为undefined
。
这是我的网格模型:
public class IngredientViewModel {
public string EncryptedIngredientId { get; set; }
public string CasNumber { get; set; }
public string IngredientName { get; set; }
[UIHint("PercentWeightEditor")]
public PercentInfoViewModel PercentInfo { get; set; }
}
以下是PercentInfoViewModel
中使用的IngredientViewModel
模型:
public class PercentInfoViewModel {
public decimal? PercentWeight { get; set; }
public string PercentUnit { get; set; }
}
这是网格部分的.cshtml:
@(Html.Kendo().Grid<IngredientViewModel>(Model)
.Name("IngredientsGrid")
.Editable(editable => editable.Mode(GridEditMode.InLine).Enabled(true))
.BindTo(Model)
.DataSource(ds => ds
.Ajax()
.ServerOperation(false)
.Events(ev => ev.Error("onGridError"))
.Model(m => {
m.Id(p => p.EncryptedIngredientId);
m.Field(p => p.EncryptedIngredientId).Editable(false);
m.Field(p => p.PercentInfo);
})
.Read("IngGrid_Read", "Company")
.Update("IngGrid_Update", "Company")
.Create("IngGrid_Create", "Company")
.Destroy("IngGrid_Destroy", "Company"))
.ToolBar(tbar => tbar.Create())
.Columns(c => {
c.Bound(m => m.CasNumber);
c.Bound(m => m.IngredientName);
c.Bound(m => m.PercentInfo).ClientTemplate("#= PercentInfo.PercentWeight # #= PercentInfo.PercentUnit #");
c.Command(command => {
command.Edit();
command.Destroy();
}).Width(200);
})
)
这是PercentWeightEditor .cshtml:
@model SupplierPortalWebsite.Models.PercentInfoViewModel
@Html.TextBoxFor(m => m.PercentWeight)
@Html.TextBoxFor(m => m.PercentUnit)
我做错了什么?
答案 0 :(得分:0)
我不得不修改网格设置的Model
调用,将空模型作为默认值。下面是代码的新部分:
.Model(m => {
m.Id(p => p.EncryptedIngredientId);
m.Field(p => p.EncryptedIngredientId).Editable(false);
m.Field(p => p.PercentInfo)
/* I added this line --> */ .DefaultValue(new PercentInfoViewModel());
})
这种方式总是有一个空模型,自定义编辑器可以为其分配属性,而不是尝试分配给不存在的对象(导致异常的原因)。