Blazor multiple NumericUpDownField binding using MatBlazor

242 Views Asked by At

I'm new to Blazor, and generally new to frontend development.

I'm trying to implement webstore card ordering, which is representing single product. Moreover, I want to have MatNumericUpDownField on each card, but I don't want to create hundred fields that will be bind to hundred NumericUpDown fields. I need some dynamic solution.

This is my code:

ProductCard

@page "/productcard"
@inherits ProductCardBase

@if (productsList == null)
{
    <div class="spinner"></div>
}
else
{
    <div class="card-deck">
        @foreach (var p in productsList)
        {
        <div class="card m-3" style="min-width: 18rem; max-width:30.5%;">
            <div class="card-header">
                <h3>@p.Caption</h3>
            </div>
            <div class="card-body text-center">
                <h4>Product ID : @p.ProductID</h4>
            </div>

            <img class="" src="@p.Pictures.FirstOrDefault()" />


            @if (p.Pictures.Count > 0)
            {
                <img class="card-img-top" src="@convertImageToDisplay(p.Pictures.FirstOrDefault().PictureDisplay.ToString())" />
            }


            <p>
                <MatNumericUpDownField Label="Quantity"
                                       @bind-Value=@testingNullableDecimal2
                                       DecimalPlaces=0
                                       Minimum=0 Maximum=null>
                </MatNumericUpDownField>
            </p>


            <div class="card-footer text-center">
                <a href=""
                   class="btn btn-primary m-1">View</a>

                <a href="#" class="btn btn-primary m-1">Edit</a>

                <a href="#" class="btn btn-danger m-1">Delete</a>
            </div>

        </div>

        }
    </div>

ProductCardBase

public class ProductCardBase : ComponentBase
{
    [Inject]
    IApiCallService<Product> apiCallService { get; set; }


    Product product { get; set; }
    public bool Result { get; set; }
    public string RecordName { get; set; }
    //public string[] Products { get; set; }
    public bool IsGridViewFiltered { get; set; }
    bool isLoading = true;
    public string SelectedBrand { get; set; }

    protected decimal? testingNullableDecimal2 = null;


    public bool IsVisible { get; set; }

    protected List<Product> productsList { get; set; }


    protected override async Task OnInitializedAsync()
    {
        product = new Product();

        await LoadProducts();
    }


   protected void Click(MouseEventArgs e)
    {
       
        testingNullableDecimal2 += 100;
    }

    private async Task LoadProducts()
    {
        await Task.Delay(500);

        productsList = await apiCallService.GetAllAsList("products");

    }

    protected string convertImageToDisplay(string image)
    {

        if (image != null)
        {
            var fs = string.Format("data:image/jpg;base64,{0}", image);
            return fs;
        }
        return "";
    }
}

In this example testingNullableDecimal2 is bind to every card, but I need to separate it for every single card.

2

There are 2 best solutions below

0
On BEST ANSWER

One solution is to put the bind variable (testingNullableDecimal) inside your Product model.

Then you change the code like this:

<MatNumericUpDownField Label="Quantity"
                       @[email protected]
                       DecimalPlaces=0
                       Minimum=0 Maximum=null>
</MatNumericUpDownField>

BTW, I am not familiar with the MatBlazor tools, but setting the max value=null looks suspicious. Is that the right syntax?

0
On

Jason D's option is good. A technique that I use a lot is to pass Lists of interfaces rather than implementations. Then you can have an extension that exposes display stuff: counters, booleans for whether stuff is selected, and so on.