Skip to content

Commit

Permalink
Solve Blazor SSR EditForm issues
Browse files Browse the repository at this point in the history
  • Loading branch information
frank-liu-toronto committed Jun 6, 2024
1 parent 9c60501 commit 04d688e
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 5 deletions.
12 changes: 10 additions & 2 deletions IMS.WebApp/Components/Pages/Inventories/AddInventory.razor
Original file line number Diff line number Diff line change
Expand Up @@ -46,11 +46,19 @@
@code {

[SupplyParameterFromForm]
private Inventory inventory { get; set; } = new Inventory();
private InventoryViewModel inventory { get; set; } = new InventoryViewModel();

private async Task Save()
{
await AddInventoryUseCase.ExecuteAsync(inventory);
var inv = new Inventory
{
InventoryId = inventory.InventoryId,
InventoryName = inventory.InventoryName,
Price = inventory.Price,
Quantity = inventory.Quantity
};

await AddInventoryUseCase.ExecuteAsync(inv);

NavigationManager.NavigateTo("/inventories");
}
Expand Down
25 changes: 22 additions & 3 deletions IMS.WebApp/Components/Pages/Inventories/EditInventory.razor
Original file line number Diff line number Diff line change
Expand Up @@ -53,18 +53,37 @@
public int InvId { get; set; }

[SupplyParameterFromForm]
private Inventory? inventory { get; set; }
private InventoryViewModel? inventory { get; set; }

protected override async Task OnParametersSetAsync()
{
this.inventory ??= await ViewInventoryByIdUseCase.ExecuteAsync(this.InvId);
if (this.inventory is null)
{
var inv = await ViewInventoryByIdUseCase.ExecuteAsync(this.InvId);
inventory = new InventoryViewModel
{
InventoryId = inv.InventoryId,
InventoryName = inv.InventoryName,
Price = inv.Price,
Quantity = inv.Quantity
};
}

}

private async Task Update()
{
if (inventory is not null)
{
await EditInventoryUseCase.ExecuteAsync(inventory);
var inv = new Inventory
{
InventoryId = inventory.InventoryId,
InventoryName = inventory.InventoryName,
Price = inventory.Price,
Quantity = inventory.Quantity
};

await EditInventoryUseCase.ExecuteAsync(inv);
NavigationManager.NavigateTo("/inventories");
}
}
Expand Down
20 changes: 20 additions & 0 deletions IMS.WebApp/ViewModels/InventoryViewModel.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
using System.ComponentModel.DataAnnotations;

namespace IMS.CoreBusiness
{
public class InventoryViewModel
{
public int InventoryId { get; set; }

[Required]
[StringLength(150)]
public string InventoryName { get; set; } = string.Empty;

[Range(0, int.MaxValue, ErrorMessage = "Quantity must be greator or equal to 0.")]
public int Quantity { get; set; }

[Range(0, int.MaxValue, ErrorMessage = "Price must be greator or equal to 0.")]
public double Price { get; set; }

}
}

0 comments on commit 04d688e

Please sign in to comment.