We use cookies to give you the best experience on our website. If you continue to browse, then you agree to our privacy policy and cookie policy. Image for the cookie policy date
close icon

Edit grid with Foreign Keys

I have a grid that is using a data source with a foreign key (department column). When I try to edit the last record (Name4) the values change to the first record in the grid and even if I change the values nothing gets updated. The drop down option when editing the record is working, but the selection is not being saved.

My Razor code for the grid is:

@model IEnumerable<XXXCore.Models.Employee>

@{
    ViewData["Title"] = "Index";
}

<h2>Index</h2>

<p>
    <a asp-action="Create">Create New</a>
</p>

 <ej-grid id="FlatGrid" allow-sorting="true" allow-paging="true" allow-reordering="true"  datasource="ViewBag.DataSource1">
     <e-edit-settings allow-adding="true" allow-editing="true" allow-deleting="true" edit-mode="@(EditMode.Normal)"></e-edit-settings>
     <e-toolbar-settings show-toolbar="true" toolbar-items="@(new List<string>() {"add","edit","delete","update","cancel" })"></e-toolbar-settings>  
    <e-columns>
        <e-column field="Name" header-text="Name" text-align="Left" width="70"></e-column>
        <e-column field="LastName" header-text="Last Name" text-align="Left" width="80"></e-column>
        <e-column field="DOB" header-text="Birth Date" text-align="Left" width="80" format="{0:dd/MM/yyyy}"></e-column>
        <e-column field="DepartmentID" datasource="(IEnumerable<object>)ViewBag.DataSource2"  
                header-text="Department" foreign-key-field="ID" foreign-key-value="Name" text-align="Left" 
                  width="80" edit-type="@(EditingType.Dropdown)"></e-column>
    </e-columns>
</ej-grid>

 
The controller code is:

        // GET: Employees
        public async Task<IActionResult> Index()
        {
            ViewBag.DataSource1 = await _context.Employees.ToListAsync();
            ViewBag.DataSource2 = await _context.Departments.ToListAsync();
            return View();
        }




What am I missing ? thanks in advance.

6 Replies

JK Jayaprakash Kamaraj Syncfusion Team June 26, 2017 05:40 AM UTC

Hi Juan, 

Thank you for contacting Syncfusion support. 

The editing is performed based on the primary key column but you have missed to refer isPrimaryKey property in column. So, please set isPrimaryKey property for primary key column and refer to the following help document, Knowledgebase document and code example 
 



<e-columns> 
    <e-column field="OrderID" header-text="Order ID" is-primary-key="true" type="number" validation-rules='new Dictionary<string, object>() { {"required",true}, {"number",true} }' text-align="Right" width="75"></e-column> 
    <e-column field="CustomerID" header-text="Customer ID" type="string" validation-rules='new Dictionary<string, object>() { {"required",true}, {"minlength",3} }' width="80"></e-column> 
    <e-column field="EmployeeID_FirstName" header-text="Employee ID" type="string" datasource="ViewBag.data" edit-type="@(EditingType.Dropdown)" text-align="Right" width="75"></e-column> 
    <e-column field="Verified" header-text="Verified" default-value="false" edit-type="Boolean"  type="boolean" width="110"  text-align="Left"></e-column> 
    </e-columns> 

Regards, 

Jayaprakash K. 



LE Leon June 27, 2017 06:43 PM UTC

it is working now thank you very much



JK Jayaprakash Kamaraj Syncfusion Team June 28, 2017 08:37 AM UTC

Hi Juan, 
 
We are happy that the problem has been solved. 
 
Please get back to us if you need any further assistance.   
 
Regards, 
 
Jayaprakash K. 
 



LE Leon August 13, 2017 03:00 PM UTC

This scenario is working but I am having a new issue. I am using a master detail grid to assign roles to an user.  I am trying to populate the table AspNetUserRoles which has to columns as the primary key (UserId and Role Id).

I am doing something similar to :

https://www.syncfusion.com/forums/126952/master-detail-grid-for-asp-net-core

I an getting the records in the detail but they are not being displayed in the screen. When I add a record it works and the new record is added to the AspNetUserRoles table in the database, but when I refresh the child grid shows the new row but all the cells are empty.

What is the way to handle a many to may relation  in a master detail grid?



LE Leon August 13, 2017 03:19 PM UTC

One of the issues I see in this case is that you can not use the  is-primary-key="true" in two columns.



AS Alan Sangeeth S Syncfusion Team August 14, 2017 12:13 PM UTC

Hi Juan, 

We are unable to reproduce the issue “Detail grid not populated with two primarykey columns” in our end. We have prepared a sample based on your requirement and the same can be downloaded from below link. 


Could you please share the below details so that we could provide you solution as early as possible? 
1.       Complete Grid code example. 
2.       Screenshot of the issue. 
3.       Please replicate the issue in above sample and send us back 

Regards,
Alan Sangeeth S 


Loader.
Live Chat Icon For mobile
Up arrow icon