-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathIndex.razor
88 lines (84 loc) · 3.36 KB
/
Index.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
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
@page "/"
@using AutoMapper
@using InstantFeedback.Models;
@using Microsoft.EntityFrameworkCore
@using DevExpress.Data.Linq
@inject IDbContextFactory<NorthwindContext> NorthwindContextFactory
@implements IDisposable
<DxGrid Data="InstantFeedbackSource"
EditModelSaving="OnEditModelSaving"
DataItemDeleting="OnDataItemDeleting"
KeyFieldName="OrderId">
<Columns>
<DxGridCommandColumn />
<DxGridDataColumn FieldName="ShipName" />
<DxGridDataColumn FieldName="ShipCity" />
<DxGridDataColumn FieldName="ShipCountry" />
<DxGridDataColumn FieldName="Freight" />
<DxGridDataColumn FieldName="OrderDate" />
<DxGridDataColumn FieldName="ShippedDate" />
</Columns>
<EditFormTemplate Context="editFormContext">
<DxFormLayout>
<DxFormLayoutItem Caption="Ship Name:">
@editFormContext.GetEditor("ShipName")
</DxFormLayoutItem>
<DxFormLayoutItem Caption="Ship City:">
@editFormContext.GetEditor("ShipCity")
</DxFormLayoutItem>
<DxFormLayoutItem Caption="Ship Country:">
@editFormContext.GetEditor("ShipCountry")
</DxFormLayoutItem>
<DxFormLayoutItem Caption="Freight:">
@editFormContext.GetEditor("Freight")
</DxFormLayoutItem>
<DxFormLayoutItem Caption="Order Date:">
@editFormContext.GetEditor("OrderDate")
</DxFormLayoutItem>
<DxFormLayoutItem Caption="Shipped Date:">
@editFormContext.GetEditor("ShippedDate")
</DxFormLayoutItem>
</DxFormLayout>
</EditFormTemplate>
</DxGrid>
@code {
EntityInstantFeedbackSource InstantFeedbackSource { get; set; }
NorthwindContext Northwind { get; set; }
IMapper ProductMapper { get; set; }
protected override void OnInitialized() {
Northwind = NorthwindContextFactory.CreateDbContext();
InstantFeedbackSource = new EntityInstantFeedbackSource(e => {
e.KeyExpression = "OrderId";
e.QueryableSource = Northwind.Orders;
});
var config = new MapperConfiguration(c => c.CreateMap<Order, Order>());
ProductMapper = config.CreateMapper();
}
async Task OnEditModelSaving(GridEditModelSavingEventArgs e) {
var editModel = (Order)e.EditModel;
// Re-query a data item from the database.
var dataItem = e.IsNew ? new Order() : Northwind.Orders.Find(editModel.OrderId);
// Assign changes from the edit model to the data item.
if (dataItem != null) {
ProductMapper.Map<Order, Order>(editModel, dataItem);
// Post changes to the database.
if (e.IsNew)
await Northwind.AddAsync(dataItem);
await Northwind.SaveChangesAsync();
}
}
async Task OnDataItemDeleting(GridDataItemDeletingEventArgs e) {
// Re-query a data item from the database.
var orderId = e.Grid.GetDataItemValue(e.DataItem, "OrderId");
var dataItem = Northwind.Orders.Find(orderId);
if (dataItem != null) {
// Remove the data item from the database.
Northwind.Remove(dataItem);
await Northwind.SaveChangesAsync();
}
}
public void Dispose() {
InstantFeedbackSource?.Dispose();
Northwind?.Dispose();
}
}