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

Insert using GridForeignKey not working

1 Answer 117 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Lisa
Top achievements
Rank 1
Lisa asked on 16 Feb 2013, 06:04 PM

I have my kendo.grid using the grideditmode.popup for creating and editing.  My foreign key values show fine in the grid and also on an edit in the drop downs.  But they won't add the actual values into the db.  So I add a new row and pick the two items in the two drop downs and they remain null in the db.  If I edit one where they are null, they stay null.  If I edit one where they aren't null and change the values, that works fine.  Any ideas are appreciated.

columns.ForeignKey(item => item.InstructorID, (IEnumerable<PointsInfrastructure.Instructor>)ViewData["instructorList"], "InstructorID", "Name");
columns.ForeignKey(item => item.SchoolID, (IEnumerable<PointsInfrastructure.School>)ViewData["schoolList"], "SchoolID", "Name");
 
 
 
[Display(Name = "Instructor")]
[UIHint("GridForeignKey")]
public long? InstructorID { get; set; }
 
[Display(Name = "School")]
[UIHint("GridForeignKey")]
public long? SchoolID { get; set; }
 
 
 
 
            // create new tournament
            var listValue = new Competitor()
            {
                MemberID = inViewModel.MemberID,
                LastName = inViewModel.LastName,
                FirstName = inViewModel.FirstName,
                MemberNumber = inViewModel.MemberNumber,
                DateOfBirth = inViewModel.DateOfBirth,
                InstructorID = inViewModel.InstructorID,
                SchoolID =  inViewModel.SchoolID,
                Email = inViewModel.Email,
                Sex = inViewModel.Sex,
                Notes = inViewModel.Notes
            };
 
            _db.Competitors.Add(listValue);
            _db.SaveChanges();

1 Answer, 1 is accepted

Sort by
0
Vladimir Iliev
Telerik team
answered on 19 Feb 2013, 09:35 AM
Hi Lisa,

 
Basically editing nullable field using the ForeignKeyColumn is not supported out-of-the-box, however you can use the save event of the grid to allow updating of null value:

Save event handler:

function onSave(e) {
    //change EmployerId with your field name
    //If current value is null
    if (!e.model.EmployerId) {
        //change the model value
        e.model.EmployerId = 0;
        //get the currently selected value from the DDL
        var currentlySelectedValue = $(e.container.find('[data-role=dropdownlist]')[0]).data().kendoDropDownList.value();
        //set the value to the model
        e.model.set('EmployerId', currentlySelectedValue);
    }
}
Kind Regards,
Vladimir Iliev
the Telerik team
Join us on our journey to create the world's most complete HTML 5 UI Framework - download Kendo UI now!
Tags
Grid
Asked by
Lisa
Top achievements
Rank 1
Answers by
Vladimir Iliev
Telerik team
Share this question
or