Skip to content

Blazor Component Cannot Be Updated By Parent #22180

@mysteryx93

Description

@mysteryx93

Using the following code, I'm binding model properties for display in Blazor.

Updating Coupon.Message and Coupon.Error does display it as expected.

However, changing Products[0].Price doesn't update the display. Only difference I can see is that it's part of a collection.

If it's a similar issue as in WPF, then I tried replacing List with ObservableCollection but that's not helping.

Why is the update working for direct properties but not when part of collections?

I'm also curious -- how is it keeping track of property changes? In WPF, there is INotifyPropertyChanged and ObservableCollection, and none of that in Blazor yet it's still working. What's the magic behind it? (and why isn't that magic being used in WPF)

<p class="blue">Order Summary</p>
<div class="grid gridheader gridcolProduct">Product</div>
<div class="grid gridheader gridcolQuantity">Quantity</div>
<div class="grid gridheader gridcolPrice">Price</div>
<div class="grid gridheader gridcolTotal">Total</div>
<div class="clear"></div>
@foreach (var item in Products)
{
    <div class="grid gridcolProduct">@(item.DisplayName ?? item.Name)</div>
        @if (item.AllowEditQuantity)
        {
            <RadzenNumeric @bind-Value="item.Quantity" Min="1" Max="100" Style="width:100%" />
        }
        else
        {
            @item.Quantity
        }
    <div class="grid gridcolPrice">
        @if (item.StandardPrice.HasValue && item.StandardPrice != item.Price)
        {
            <s>$@item.StandardPrice</s>
        }
        &nbsp;$@item.Price
    </div>
    <div class="grid gridcolTotal">$@(item.Price * item.Quantity)</div>
    <div class="clear"></div>
}
<div class="grid gridtotaltext">Grand Total: </div>
<div class="grid gridtotal">$@Total USD</div>
<div class="clear">&nbsp;</div>

<EditForm Model="@Coupon" OnValidSubmit="CouponSubmitAsync" class="form">
    <div style="margin-bottom: 1rem" class="row">
        <Field For="@(() => Coupon.CouponCode)" Width="4" Required="true">
            <RadzenTextBox @bind-Value="@Coupon.CouponCode" class="form-control" id="CouponCode" />
        </Field>
        <RadzenButton ButtonType="ButtonType.Submit" Text="Apply" ButtonStyle="ButtonStyle.Light" />
        <div>@Coupon.Message</div>
        <div class="alert">@Coupon.Error</div>
    </div>
</EditForm>
<p>&nbsp;</p>

@code {
public CouponModel Coupon { get; set; } = new CouponModel();
public class CouponModel
{
    public string? CouponCode { get; set; }
    public string? Message { get; set; }
    public string? Error { get; set; }
}

[Parameter]
[SuppressMessage("Usage", "CA2227:Collection properties should be read only", Justification = "Reviewed: Allow set via attribute")]
public List<OrderFormProduct> Products { get; set; } = new List<OrderFormProduct>();

.....
}

The submit button calls this code which "should" edit the displayed quantity.

public void ValidateCoupon(ValidatingCouponEventArgs e)
{
    if (e.CouponCode == "valid")
    {
        e.Products[0].Price = 147;
        e.Message = "10% discount!";
        e.IsValid = true;
    }
    else
    {
        e.Products[0].Price = 296;
    }
}

Curiously, if I edit the quantity via the RadzenNumeric, both the total and grand total get updated in the browser! Until I click "submit coupon" and it reverts back to quantity 1 and the original price.

Metadata

Metadata

Assignees

No one assigned

    Labels

    DocsThis issue tracks updating documentationarea-blazorIncludes: Blazor, Razor Components

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions