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

Foreign key column using nullable<int> not working with GridEditMode.PopUp

9 Answers 663 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Stuart
Top achievements
Rank 1
Stuart asked on 01 Aug 2018, 02:45 PM

Hi,

I'm having an issue getting a foreign key column to persist in the grid. Here's my grid...

@(Html.Kendo().Grid<GridDataItem>(Model.GridDataItems)
    .Name("gridDataItems")
    .Columns(columns =>
    {
      columns.Bound(model => model.Name);
      columns.ForeignKey(model => model.ChildDataItemId, Model.AllChildDataItems, "Id", "Value");//.EditorTemplateName("_ForeignKeyDropDown");
      columns.Command(command =>
      {
        command.Edit();
      }).Width(172);
    })
    .ToolBar(toolbar => toolbar.Create())
    .Editable(editable => editable.Mode(GridEditMode.PopUp))
    .DataSource(dataSource => dataSource
      .Ajax()
      .PageSize(20)
      .ServerOperation(false)
      .Model(model =>
      {
        model.Id(p => p.Id);
      })
    )
  )

Here's the controller...

public class GridTestController : Controller
    {
        public IActionResult Index()
        {
            var viewModel = new GridTestModel
            {
                GridDataItems = new List<GridDataItem>
                {
                    new GridDataItem
                    {
                        Id = 1,
                        Name = "Record 1",
                        ChildDataItemId = 2
                    },
                    new GridDataItem
                    {
                        Id = 2,
                        Name = "Record 2",
                        ChildDataItemId = 3
                    },
                    new GridDataItem
                    {
                        Id = 3,
                        Name = "Record 3"
                    }
                }
            };
 
            return View(viewModel);
        }
    }

and here's the Model...

public class GridTestModel
    {
        public List<GridDataItem> GridDataItems { get; set; }
 
        public List<ChildDataItem> AllChildDataItems
        {
            get
            {
                return new List<ChildDataItem>
                {
                    new ChildDataItem
                    {
                        Id = 1,
                        Value = "Option 1"
                    },
                    new ChildDataItem
                    {
                        Id = 2,
                        Value = "Option 2"
                    },
                    new ChildDataItem
                    {
                        Id = 3,
                        Value = "Option 3"
                    }
                };
            }
        }
    }
 
    public class GridDataItem
    {
        public int Id { get; set; }
        public string Name { get; set; }
        public int? ChildDataItemId { get; set; }
        public ChildDataItem ChildDataItem { get; set; }
    }
 
    public class ChildDataItem
    {
        public int Id { get; set; }
        public string Value { get; set; }
    }

The values for Option 1 and Option 2 can be updated fine (as they're preset with a non-null value) but I'm not able to select a value for the Option 3 record nor am I able to set the foreign key value (ChildDataItemId) for a new record.

Anyone an idea as to why? I've also tried a custom editor template for the drop down field using .ValuePrimitive(true) but this has no effect.

Thanks
Stuart.

9 Answers, 1 is accepted

Sort by
0
Viktor Tachev
Telerik team
answered on 06 Aug 2018, 11:09 AM
Hi Stuart,

Please make sure that you specify a default value for the ChildDataItemId field. This way when a new item is created the default value will be used to populate the editor initially. Check out the example below for reference.



Regards,
Viktor Tachev
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
Stuart
Top achievements
Rank 1
answered on 06 Aug 2018, 12:18 PM

Hi Viktor,

I don't think that's going to work in our case. The foreign key value is allowed to be NULL in the database, if we set a default value then it's going to get that value even if the user doesn't select an option.

  • Setting the default to null also doesn't work - as before it's always null and doesn't update to reflect what the user selects.
  • Setting it to zero means we get foreign key constraint error.
  • Setting it to a non-zero value means we get a value saved even when it's supposed to be null.

Cheers,
Stuart.

0
Viktor Tachev
Telerik team
answered on 09 Aug 2018, 07:46 AM
Hello Stuart,

Would you send us a sample project where the behavior is replicated? This will enable us to examine the behavior locally and look for its cause. 

Regards,
Viktor Tachev
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
Stuart
Top achievements
Rank 1
answered on 09 Aug 2018, 10:05 AM

Hi Viktor,

Sure, it's around 26MB, what's the best way to get it to you?

Thanks,
Stuart.

0
Alex Hajigeorgieva
Telerik team
answered on 14 Aug 2018, 06:10 AM
Hello, Stuart,

My name is Alex and I am filling in for my colleague Viktor while he is away.

The ticketing system allows up to 40MB to be uploaded az zip or RAR. In case there is sensitive information, please open a private support ticket and send it to us as an attachment. Alternatively, attach it to your next response here, just delete the bin and obj folders, the Kendo.Mvc dll (if present) and any non-minified source files that you may have included.

Kind Regards,
Alex Hajigeorgieva
Progress Telerik
Get quickly 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
Stuart
Top achievements
Rank 1
answered on 16 Aug 2018, 11:46 AM

For anyone else that encounters this problem, the resolution was to add a UIHint to the nullable<int> property.

e.g.

[UIHint("GridForeignKey")]
public int? ChildDataItemId { get; set; }

 

Thanks again Alex!

Stuart.

0
Vince
Top achievements
Rank 1
answered on 05 Feb 2019, 08:15 PM
I have the same problem as you this solution isn't working :(
0
Viktor Tachev
Telerik team
answered on 08 Feb 2019, 08:37 AM
Hello Vince,

Would you open a support ticket and attach a runnable sample where the behavior you are observing can be reproduced? This will enable us to examine it and try to figure out what is causing the issue. 


Regards,
Viktor Tachev
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
huangjia
Top achievements
Rank 1
Iron
answered on 23 Nov 2022, 08:31 AM
I have the same problem
Aleksandar
Telerik team
commented on 25 Nov 2022, 09:12 AM

Hello, did you try adding the [UIHint] attribute as suggested in the post above? If this is has not resolved the issue consider sharing more details - the Grid configuration, model definition, endpoint details and last but not least what actually is not working.
Tags
Grid
Asked by
Stuart
Top achievements
Rank 1
Answers by
Viktor Tachev
Telerik team
Stuart
Top achievements
Rank 1
Alex Hajigeorgieva
Telerik team
Vince
Top achievements
Rank 1
huangjia
Top achievements
Rank 1
Iron
Share this question
or