Skip to content

How to fix "Sequence contains no matching element" in Sales/Index.cshtml #1

@freeburma

Description

@freeburma

Hi there,

If some one get "Sequence contains no matching element" in Sales/Index.cshtml, you should add the following code. This happened because when the buyer cancelled the payment. The error said "The index you requested was not found in the array" in plain English.
This will happened at line 40:

saleserror

To Fix this: "Add the IF STATEMENT "if (payment.transactions[0].related_resources.Count != 0)"as below or cut line 33-56 and paste with this code snipped.

@foreach (var payment in Model.payments)
{
// Checking for the completed transitions
if (payment.transactions[0].related_resources.Count != 0)
{

@payment.id
@payment.create_time
@payment.state
$@(decimal.Parse(payment.transactions.First().amount.total).ToString("0.00"))

                                <td class="text-right">@(payment.transactions.FirstOrDefault().related_resources.FirstOrDefault(x => x.sale != null).sale.state)</td>

                                <td class="text-right">

                                    <!-- Checking the refunded sales. If it was refunded, the button will disable. -->
                                    @if (payment.transactions.FirstOrDefault().related_resources.FirstOrDefault(x => x.sale != null).sale.state == "refunded")
                                    {
                                    <!-- Button was disabled. -->
                                        <button type="button" class="btn btn-success" disabled>Refunded</button>
                                    }
                                    else
                                    {
                                        using (Html.BeginForm("Refund", "Sales", FormMethod.Post))
                                        {
                                        <!-- Looking for Sales ID -->
                                            @Html.Hidden("saleId", payment.transactions.First().related_resources.First(x => x.sale != null).sale.id)
                                            <button type="submit" class="btn btn-warning">Refund</button>
                                        }
                                    }
                                </td>
                            </tr>
                        }
                    }

Hope, this helps to someone.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions