带参数的模态在被取消后仍然应用更改
在问题之后,下面的代码显示了一个模态,我可以在其中编辑有关机器的信息,问题是当我对表单中的参数应用更改并点击取消时,更改将应用于参数,因此与我会点击更新。
也许它很简单,但我只是没有看到它,我对 C# 和 Blazor 还比较陌生。如果有人可以提供帮助,那就太好了:)
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();
}
}
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();
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您正在使用此行创建参数,
并使用此行代码将参数发送到模态形式,并且在模态形式中,参数绑定到输入控件。
但是,对象是通过引用传递的,并且通过击键或任何其他方式对其属性所做的任何更改都会即使您取消编辑,也可以更改原点中的对象属性。
解决此问题的一种方法是创建参数的新副本并将其发送到模式形式
您可以使用 Newtonsoft 库创建 machineInfo 对象的副本,或者可以使用任何其他 JSON 序列化程序
You are creating parameters with this line
And sending parameters to modal form with this line of code, and in modal form parameters are bind to input controls
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