I've spent 2 hours on this one and can't seem to figure it out. I have lists of original and modified objects (I'm not using Entity Framework tracking). The modified list is bound to a RadzenGrid. The way I'm setting the modified list should cut all references to the original. When I edit a row and handle the SaveRow event, both the original and modified lists' object is changed. It's not making sense to me, I tried every C# trick to cut the reference. Here's the code:
<RadzenGrid @ref="controllerGrid" Data="@modifiedControllers" TItem="PinnacleController" AllowFiltering="true" FilterCaseSensitivity="FilterCaseSensitivity.CaseInsensitive"
FilterMode="FilterMode.Simple" AllowPaging="true" AllowSorting="true" PageSize="9" EditMode="DataGridEditMode.Multiple" RowCreate="@InsertRow" CellRender="@CellRender">
<RadzenTextBox @bind-Value="controller.Name" Style="width:97%" />
<button class="select-btn" @onclick="@(args => EditRow(controller))">Edit
<button class="select-btn" @onclick="@((args) => SaveRow(controller))">Save
<button class="delete-btn" @onclick="@((args) => CancelEdit(controller))">Cancel
<button class="delete-btn" @onclick="@(args => DeleteRow(controller))">Delete
<button class="delete-btn" @onclick="@(args => DeleteRow(controller))">Delete
<button class="select-btn" @onclick="@(args => SelectRow(controller))">Select
@code {
private RadzenGrid controllerGrid;
private List modifiedControllers;
private List originalControllers;
private List<RecordModification> pendingTransactions = new List<RecordModification>();
protected override async Task OnInitializedAsync()
{
await GetControllers();
}
private async Task GetControllers()
{
originalControllers = await httpClient.GetFromJsonAsync<List<PinnacleController>>(httpClient.BaseAddress + "myapicall");
modifiedControllers = new List<PinnacleController>(originalControllers);
}
private void EditRow(PinnacleController controller)
{
controllerGrid.EditRow(controller);
}
private void SaveRow(PinnacleController controller)
{
PinnacleController orig = (from x in originalControllers where x.Id == x.Id select x).First();
if (orig.Name != controller.Name)
{
controllerGrid.UpdateRow(controller);
pendingTransactions.Add(new RecordModification<PinnacleController>()
{
Modified = controller,
Original = orig,
Rowid = controller.Rowid,
RecordStatus = RecordStatus.Updated
});
}
else
{
controllerGrid.CancelEditRow(controller);
}
}
}