Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<Project Sdk="Microsoft.NET.Sdk.BlazorWebAssembly">

<PropertyGroup>
<TargetFramework>net7.0</TargetFramework>
<TargetFramework>net10.0</TargetFramework>
<Nullable>enable</Nullable>
<ImplicitUsings>enable</ImplicitUsings>
</PropertyGroup>
Expand All @@ -13,8 +13,8 @@
<ExternalConsole>true</ExternalConsole>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Microsoft.AspNetCore.Components.WebAssembly" Version="7.0.7" />
<PackageReference Include="Microsoft.AspNetCore.Components.WebAssembly.DevServer" Version="7.0.7" PrivateAssets="all" />
<PackageReference Include="Microsoft.AspNetCore.Components.WebAssembly" Version="10.0.10" />
<PackageReference Include="Microsoft.AspNetCore.Components.WebAssembly.DevServer" Version="10.0.10" PrivateAssets="all" />
</ItemGroup>

</Project>
Original file line number Diff line number Diff line change
Expand Up @@ -36,16 +36,35 @@
<option value="@topping.Id">@topping.Name +$@topping.Price</option>
}
</select>
<br/>
<span>Fourth topping (InputSelect inside EditForm):</span>
<EditForm Model="order">
<InputSelect @bind-Value="order.ToppingId" @bind-Value:after="RecalculateTotal">
@foreach (var topping in toppings)
{
<option value="@topping.Id">@topping.Name</option>
}
</InputSelect>
</EditForm>
<p>Fourth topping total: $@fourthTotal</p>

@code
{
public record Topping(int Id, string Name, double Price);

public class Order
{
public int ToppingId { get; set; }
}

public string? selectedTopping;
public string? selectedSecondTopping;
public string? selectedThirdTopping;
public static double baseBurgerCost = 5.4;
public double totalCost = baseBurgerCost;
public double grandTotal;
public double fourthTotal;
public Order order = new();

public List<Topping> toppings = new List<Topping>()
{
Expand All @@ -59,26 +78,37 @@

public void HandleChange(ChangeEventArgs args)
{
@foreach (var topping in toppings)
if (!int.TryParse(args.Value?.ToString(), out var id))
{
if (topping.Id == int.Parse(args.Value.ToString()))
{
selectedSecondTopping = topping.Name;
totalCost = Math.Round(baseBurgerCost + topping.Price, 2);
}
return;
}

var topping = toppings.FirstOrDefault(t => t.Id == id);
if (topping is null)
{
return;
}

selectedSecondTopping = topping.Name;
totalCost = Math.Round(baseBurgerCost + topping.Price, 2);
}

public async Task CalculateGrandTotal()
{
await Task.Delay(2000);

@foreach (var topping in toppings)
foreach (var topping in toppings)
{
if (topping.Id == int.Parse(selectedThirdTopping))

Check warning on line 102 in blazor-features/OnChangeEventWithSelectDropdown/OnChangeEventWithSelectDropdown/Pages/Index.razor

View workflow job for this annotation

GitHub Actions / Build & test (blazor-features/OnChangeEventWithSelectDropdown)

Possible null reference argument for parameter 's' in 'int int.Parse(string s)'.

Check warning on line 102 in blazor-features/OnChangeEventWithSelectDropdown/OnChangeEventWithSelectDropdown/Pages/Index.razor

View workflow job for this annotation

GitHub Actions / Build & test (blazor-features/OnChangeEventWithSelectDropdown)

Possible null reference argument for parameter 's' in 'int int.Parse(string s)'.
{
grandTotal = Math.Round(totalCost + topping.Price, 2);
}
}
}

public void RecalculateTotal()
{
var topping = toppings.FirstOrDefault(t => t.Id == order.ToppingId);
fourthTotal = topping is null ? 0 : Math.Round(baseBurgerCost + topping.Price, 2);
}
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>net7.0</TargetFramework>
<TargetFramework>net10.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>

Expand All @@ -10,11 +10,11 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.5.0" />
<PackageReference Include="MSTest.TestAdapter" Version="2.2.10" />
<PackageReference Include="MSTest.TestFramework" Version="2.2.10" />
<PackageReference Include="coverlet.collector" Version="3.2.0" />
<PackageReference Include="bunit" Version="1.22.19" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="18.8.1" />
<PackageReference Include="MSTest.TestAdapter" Version="4.3.3" />
<PackageReference Include="MSTest.TestFramework" Version="4.3.3" />
<PackageReference Include="coverlet.collector" Version="10.0.1" />
<PackageReference Include="bunit" Version="1.40.0" />
</ItemGroup>

<ItemGroup>
Expand Down
Loading