Communication_Edit.razor
3.13 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
@using DataAcquisition.Common.Enums
@using DataAcquisition.DataAccess
@using DataAcquisition.Models
@using Microsoft.EntityFrameworkCore
@using System.Text.Json
@inject IDbContextFactory<DataContext> dbContextFactory;
@inject IMessageService _message
<Modal Title="@modal_title"
Visible="Visible"
Maximizable="true"
Centered="true"
Resizable="true"
Draggable="true"
MaxBodyHeight="80vh"
DestroyOnClose="true"
OnOk="HandleOk"
OnCancel="HandleCancel">
<Form Model="@model" OnFinish="OnFinish" OnFinishFailed="OnFinishFailed" @ref="@_form">
<FormItem Label="编码" Required>
<Input @bind-Value="@context.Code" />
</FormItem>
<FormItem Label="名称" Required>
<Input @bind-Value="@context.Name" />
</FormItem>
<FormItem Label="通讯方式" Required>
<EnumSelect TEnum="CommunicationTypeConst" @bind-Value="context.CommunicationType" />
</FormItem>
<FormItem Label="IP地址" Required>
<Input @bind-Value="@context.IpAddress" />
</FormItem>
<FormItem Label="端口号" Required>
<Input @bind-Value="@context.Port" />
</FormItem>
<FormItem Label="是否启用" Required>
<Switch @bind-Value="@context.Enable" />
</FormItem>
<FormItem Label="备注">
<TextArea @bind-Value="@context.Remark" />
</FormItem>
</Form>
</Modal>
@code {
[Parameter]
public int CommunicationId { get; set; } = 0;
[Parameter]
public bool Visible { get; set; }
[Parameter]
public EventCallback<bool> VisibleChanged { get; set; }
bool _submitting = false;
[Parameter]
public EventCallback OnSubmitSuccess { get; set; }
string modal_title = string.Empty;
CommunicationConfig model = new CommunicationConfig();
private Form<CommunicationConfig> _form = null!;
protected override void OnParametersSet()
{
if (!Visible) return;
try
{
using var context = dbContextFactory.CreateDbContext();
model = context.CommunicationConfigs.Where(x => x.Id == CommunicationId).First();
modal_title = model.Name;
base.OnParametersSet();
}
catch (Exception ex)
{
_message.Error($"加载数据失败:{ex.Message}");
}
}
public void HandleOk()
{
try
{
using var context = dbContextFactory.CreateDbContext();
context.CommunicationConfigs.Update(model);
context.SaveChanges();
_form.Submit();
}
catch (Exception ex)
{
_message.Error($"保存失败:{ex.Message}");
}
}
public void HandleCancel()
{
VisibleChanged.InvokeAsync(false);
}
private void OnFinish(EditContext editContext)
{
_submitting = false;
// VisibleChanged.InvokeAsync(false);
OnSubmitSuccess.InvokeAsync();
}
private void OnFinishFailed(EditContext editContext)
{
_message.Error($"Failed:{JsonSerializer.Serialize(model)}");
}
}