1

Following Problem, the code below shows a Modal where I can edit Info about Machines, the problem is that when I apply changes to the parameters in the form, and I hit cancel, the changes will apply to the parameters anyway, so the same as I would hit update.

And maybe its something simple and I just dont see it, im relatively new to C# and Blazor.. Would be very nice if someone could help out :)

private async Task Edit Machine(PhysicalMachineInfo machineInfo)
{
var parameters = new ModalParameters();
parameters.Add(nameof(CreatePhysicalMachineModal.physicalMachine), machineInfo);
var createMachineEntryModal = Modal.Show<CreatePhysicalMachineModal>("Edit MachineInfo",parameters); 
var modalResult = await createMachineEntryModal.Result;

if (!modalResult.Cancelled)
{
_ = LoadMachines();
}
}
Carbonide
  • 13
  • 3

1 Answers1

1

You are creating parameters with this line

parameters.Add(nameof(CreatePhysicalMachineModal.physicalMachine), machineInfo);

And sending parameters to modal form with this line of code, and in modal form parameters are bind to input controls

var createMachineEntryModal = Modal.Show<CreatePhysicalMachineModal>("Edit MachineInfo",parameters);

However the objects are passed by reference and any changes made to it's properties by keystrokes or any other means will change object properties in the origin, even if you cancel editing.

One solution to this issue is create a fresh copy of parameters and send it to modal form

You can create copy of machineInfo object like this, using Newtonsoft library or can use any other JSON serializer

 string obj =  Newtonsoft.Json.JsonConvert.SerializeObject(machineInfo);
 copyMachineInfo =  Newtonsoft.Json.JsonConvert.DeserializeObject<PhysicalMachineInfo>(obj);
Surinder Singh
  • 1,165
  • 1
  • 3
  • 11
  • Alright that sounds logical, I also thought that something similar like that could be the problem. How do I create a fresh copy of the parameters and send them to modal form? Would be really nice from you if you could explain, as I said newbie here :D Greetings :) – Carbonide Mar 01 '22 at 16:18
  • I have updated my answer, have a look at it – Surinder Singh Mar 01 '22 at 16:35