EquipmentProperty_Add.razor
2.68 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
@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="@("新增")"
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>
<Input @bind-Value="@context.DataAddress" />
</FormItem>
<FormItem Label="数据类型" Required>
<EnumSelect TEnum="DataTypeConst" @bind-Value="context.DataType" />
</FormItem>
<FormItem Label="是否启用" Required>
<Switch @bind-Value="@context.Enable" />
</FormItem>
<FormItem Label="备注">
<TextArea @bind-Value="@context.Remark" />
</FormItem>
</Form>
</Modal>
@code {
[Parameter]
public EventCallback OnSubmitSuccess { get; set; }
[Parameter]
public bool Visible { get; set; }
[Parameter]
public int EquipmentId { get; set; }
[Parameter]
public EventCallback<bool> VisibleChanged { get; set; }
private Form<EquipmentProperty> _form = null!;
EquipmentProperty model = new EquipmentProperty();
protected override void OnParametersSet()
{
if (!Visible) return;
model = new EquipmentProperty
{
EquipmentId = EquipmentId
};
base.OnParametersSet();
}
public void HandleOk()
{
try
{
using var context = dbContextFactory.CreateDbContext();
context.EquipmentProperties.Add(model);
context.SaveChanges();
_form.Submit();
}
catch (Exception ex)
{
_message.Error($"保存失败:{ex.Message}");
}
}
public async Task HandleCancel()
{
await VisibleChanged.InvokeAsync(false);
}
private void OnFinish(EditContext editContext)
{
VisibleChanged.InvokeAsync(false);
OnSubmitSuccess.InvokeAsync();
}
private void OnFinishFailed(EditContext editContext)
{
_message.Error($"Failed:{JsonSerializer.Serialize(model)}");
}
}