I have grid with nullable int foreign key:
Create method work as expected, foreign key value is present in the model, but when i try to edit foreign key value to null, or cancel edit without setting value, i'm getting javascript error:
VM1494:3 Uncaught TypeError: Cannot set property
'Value'
of
null
at eval (eval at setter (kendo.all.js:2118), <anonymous>:3:24)
at o._set (kendo.all.js:4963)
at o.accept (kendo.all.js:5180)
at kendo.all.js:7018
at init._eachItem (kendo.all.js:6996)
at init._cancelModel (kendo.all.js:7014)
at init.cancelChanges (kendo.all.js:6880)
at init.cancelRow (kendo.all.js:61601)
at init._editCancelClick (kendo.all.js:61319)
at HTMLAnchorElement.proxy (VM1001 jquery-3.3.1.js:10268)
I tried GridForeignKey template and custom template with ValuePrimitive set to true.
Custom template:
@(
Html.Kendo().DropDownListFor(m => m)
.OptionLabel(
""
)
.AutoBind(
false
)
.BindTo((System.Collections.IEnumerable)ViewData[
"dictionaryType_Data"
])
.DataValueField(
"Id"
)
.DataTextField(
"Name"
)
.ValuePrimitive(
true
)
)
Model:
public
class
DictionaryTypeModel
{
public
DictionaryTypeModel() { }
public
int
Id {
get
;
set
; }
public
string
Code {
get
;
set
; }
public
string
Name {
get
;
set
; }
public
int
? ParentId {
get
;
set
; }
public
string
Deleted {
get
;
set
; }
}
Grid:
@(Html.Kendo().Grid<RDEapp.Models.DictionaryTypeModel>()
.Name(
"DictionaryTypeGrid"
)
.Columns(c =>
{
c.Bound(m => m.Name);
c.Bound(m => m.Code);
c.Bound(m => m.Deleted);
c.ForeignKey(m => m.ParentId, (List<RDEapp.Models.TypSlownikaModel>)ViewData[
"dictionaryType_Data"
],
"Id"
,
"Name"
)
c.Command(command =>
{
command.Edit().Text(
"Edit"
);
command.Custom(
"Delete"
).IconClass(
"k-icon k-i-delete"
).Click(
"deleteType"
);
}).Width(200);
})
.ToolBar(toolbar => toolbar.Create().Text(
"Add dictionary type"
))
.Editable(edit => edit.Mode(GridEditMode.InLine).DisplayDeleteConfirmation(
false
))
.Selectable(selectable => selectable
.Mode(GridSelectionMode.Single)
.Type(GridSelectionType.Row))
.DataSource(dataSource => dataSource
.Ajax()
.PageSize(25)
.Model(model =>
{
model.Id(u => u.Id);
model.Field(u => u.Id).Editable(
false
);
model.Field(u => u.Deleted).Editable(
false
).DefaultValue(
"N"
);
model.Field(u => u.ParentId.Value).Editable(
true
);
model.Field(u => u.Name).Editable(
true
);
model.Field(u => u.Code).Editable(
true
);
})
.Read(u => u.Action(RDEapp.Controllers.AdminController.NazwyMetod.DictionaryTypeRead, RDEapp.Controllers.AdminController.Name))
.Create(create => create.Action(
"AddDictionaryType"
, RDEapp.Controllers.AdminController.Name))
.Update(upd => upd.Action(
"EditDictionaryType"
, RDEapp.Controllers.AdminController.Name))
.Destroy(del => del.Action(
"DeleteDictionaryType"
, RDEapp.Controllers.AdminController.Name))
.Events(e => e.Error(
"crudErrors"
))
)
.Pageable()
)