-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathIndex.razor
81 lines (68 loc) · 2.88 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
@page "/"
@implements IDisposable
@using AutoMapper
@using DevExpress.Blazor.Internal
@inject IDbContextFactory<NorthwindContext> NorthwindContextFactory
@inject IGlobalOptionsService GlobalOptionsService
@if(IsLoading) {
<text>Loading...</text>
}else {
<DxGrid Data="Products"
EditMode="GridEditMode.EditRow"
CustomizeEditModel="Grid_CustomizeEditModel"
EditModelSaving="Grid_EditModelSaving">
<Columns>
<DxGridCommandColumn DeleteButtonVisible="false" Width="15%" />
<DxGridDataColumn FieldName="ProductName" Width="25%" />
<DxGridDataColumn FieldName="CategoryId" Caption="Category Name" Width="10%">
<EditSettings>
<DxComboBoxSettings Data="Categories" ValueFieldName="CategoryId" TextFieldName="CategoryName"/>
</EditSettings>
</DxGridDataColumn>
<DxGridDataColumn FieldName="UnitPrice" DisplayFormat="c" Width="10%">
<EditSettings>
<DxSpinEditSettings MinValue="0M" Mask="n3" />
</EditSettings>
</DxGridDataColumn>
<DxGridDataColumn FieldName="UnitsInStock" />
<DxGridDataColumn FieldName="QuantityPerUnit" Width="15%" />
<DxGridDataColumn FieldName="Discontinued" />
</Columns>
</DxGrid>
}
@code {
NorthwindContext Northwind { get; set; }
List<Product> Products { get; set; }
List<Category> Categories { get; set; }
IMapper ProductMapper { get; set; }
bool IsLoading { get; set; } = true;
protected override async Task OnInitializedAsync() {
var config = new MapperConfiguration(c => c.CreateMap<Product, EditableProduct>().ReverseMap());
ProductMapper = config.CreateMapper();
Northwind = NorthwindContextFactory.CreateDbContext();
Products = await Northwind.Products.ToListAsync();
Categories = await Northwind.Categories.ToListAsync();
GlobalOptionsService.GlobalOptions.ShowValidationIcon = true;
IsLoading = false;
}
void Grid_CustomizeEditModel(GridCustomizeEditModelEventArgs e) {
var editableProduct = new EditableProduct();
if(!e.IsNew)
ProductMapper.Map((Product)e.DataItem, editableProduct);
e.EditModel = editableProduct;
}
async Task Grid_EditModelSaving(GridEditModelSavingEventArgs e) {
var editableProduct = (EditableProduct)e.EditModel;
var product = e.IsNew
? new Product()
: Northwind.Products.Find(e.Grid.GetDataItemValue(e.DataItem, "ProductId"));
ProductMapper.Map(editableProduct, product);
if(e.IsNew)
await Northwind.Products.AddAsync(product);
await Northwind.SaveChangesAsync();
Products = await Northwind.Products.ToListAsync();
}
public void Dispose() {
Northwind?.Dispose();
}
}