How can I implement a price filter on my website to filter cars based on a price range ('from...

Discussão em 'Angular' iniciado por Ali Zain Khan, Setembro 12, 2024.

  1. Even though I set the price range to 'from price' = 100,000 and 'to price' = 150,000, the filter shows cars with prices outside this range. I suspect there might be an issue with how the price filtering is applied, or the data might not be correctly parsed or compared.

    Frontend Implementation:

    onInputChange(event: any, field: string): void {
    this.filter[field] = event.target.value;
    }
    applyFilter() {
    this.filter['car_type'] = "17";
    const requestPayload = { Filter: this.filter };
    this.Service.getfiltercar(requestPayload).subscribe(
    {
    next: (response: any) => {
    this.newcar = response
    },
    error: (error: any) => {
    console.error('There was an error!', error);
    }
    });
    }


    Backend Implementation:

    case "price":
    var fromPriceString = filters.FirstOrDefault(f => f.Key == "FromPrice").Value;
    var toPriceString = filters.FirstOrDefault(f => f.Key == "ToPrice").Value;

    if (decimal.TryParse(fromPriceString, out var fromPrice) && decimal.TryParse(toPriceString, out var toPrice))
    {
    query = query.Where(c => decimal.Parse(c.price) >= fromPrice && decimal.Parse(c.price) <= toPrice);
    }
    break;


    I expected that the filter would correctly display only those cars whose prices fall within the range specified by FromPrice and ToPrice.

    I also expected that the filter would correctly handle cases where either FromPrice or ToPrice is not provided.

    Continue reading...

Compartilhe esta Página