-
Notifications
You must be signed in to change notification settings - Fork 3
/
SeparateEditForm.razor
63 lines (60 loc) · 2.67 KB
/
SeparateEditForm.razor
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
@page "/SeparateEditForm/{id}"
@inject NavigationManager UriHelper
@using DataGridSeparateEditForm.Data
@inject WeatherForecastService ForecastService
@if (copyForecast == null) {
<h1>Loading...</h1>
}
else {
<DxFormLayout Data=@copyForecast ItemUpdating=@((pair) => OnItemUpdating(pair.Key, pair.Value))>
<DxFormLayoutItem Field=@nameof(WeatherForecast.TemperatureC) Caption="Temp. (C):" />
<DxFormLayoutItem Field=@nameof(WeatherForecast.Summary) Caption="Summary:">
<DxComboBox Data=@summaries
Value="@(((string)((ValueEditContext)context).Value))"
ValueChanged="@((string value) => ((ValueEditContext)context).OnChanged(value))"></DxComboBox>
</DxFormLayoutItem>
<DxFormLayoutItem Field=@nameof(WeatherForecast.Date) Caption="Date:">
<DxDateEdit Date=@(((DateTime)((ValueEditContext)context).Value))
DateChanged=@((DateTime value) => ((ValueEditContext)context).OnChanged(value))></DxDateEdit>
</DxFormLayoutItem>
<DxFormLayoutItem ColSpanMd="3" BeginRow="true">
<DxButton Id="saveButton" RenderStyle="ButtonRenderStyle.Success" Click="@(()=> { saveChanges(); })" Text="Save"></DxButton>
<DxButton Id="cancelButton" RenderStyle="ButtonRenderStyle.Secondary" Click="@(()=> { cancelChanges(); })" Text="Cancel"></DxButton>
</DxFormLayoutItem>
</DxFormLayout>
}
@code {
[Parameter]
public string? id { get; set; }
WeatherForecast? forecast = null;
WeatherForecast? copyForecast = null;
string[]? summaries;
protected override async Task OnInitializedAsync() {
forecast = await ForecastService.GetForecastByIdAsync(Convert.ToInt32(id));
copyForecast = new WeatherForecast() {
ID = forecast.ID,
Date = forecast.Date,
Summary = forecast.Summary,
TemperatureC = forecast.TemperatureC
};
summaries = ForecastService.Summaries;
}
void OnItemUpdating(string fieldName, object newValue) {
if (fieldName == nameof(WeatherForecast.Summary)) {
copyForecast.Summary = newValue.ToString();
}
else if (fieldName == nameof(WeatherForecast.TemperatureC)) {
copyForecast.TemperatureC = Convert.ToInt32(newValue);
}
else if (fieldName == nameof(WeatherForecast.Date)) {
copyForecast.Date = Convert.ToDateTime(newValue);
}
}
async void saveChanges() {
await ForecastService.ChangeForecastAsync(copyForecast);
UriHelper.NavigateTo(@"\");
}
void cancelChanges() {
UriHelper.NavigateTo(@"\");
}
}