This is a migrated thread and some comments may be shown as answers.

grid int? foreign key in line edit error

2 Answers 331 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Adam
Top achievements
Rank 1
Adam asked on 29 Jul 2019, 03:40 PM

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()
    )

2 Answers, 1 is accepted

Sort by
0
Tsvetomir
Telerik team
answered on 31 Jul 2019, 08:20 AM
Hi Adam,

I have noticed that within the model, the field is not marked as nullable. Can you ensure that it is marked as nullable, and let me know on your observations.

For your reference, I am attaching a sample project with a Kendo UI Grid. One of the columns of the grid is a foreign key column. I have set the fields to allow for null values and it appears that a null value is submitted to the remote endpoint without throwing any exceptions.

In case the issue is still present, modify the sample project and send it back to me.


Best regards,
Tsvetomir
Progress Telerik
Get quickly onboarded and successful with your Telerik and/or Kendo UI products with the Virtual Classroom free technical training, available to all active customers. Learn More.
0
Adam
Top achievements
Rank 1
answered on 31 Jul 2019, 09:03 AM
U mean thad Id field is not marked as nullable? It cannot be becouse it is Primary Key and it may cause other errors in application. I manager to get around this error by adding -1 to ParentId in Foreign Key model but now i need to check this in CRUD methods.
Tags
Grid
Asked by
Adam
Top achievements
Rank 1
Answers by
Tsvetomir
Telerik team
Adam
Top achievements
Rank 1
Share this question
or