-2

I have this html:

<div class="row">
    <div class="col-md-12">
        @if (Model.PendingTransactions != null)
        {
            <table>
                <thead>
                    <tr>
                        <th class="text-center">Select</th>
                        <th class="text-center">@Html.DisplayNameFor(m => m.PendingTransactions.First().transactionDate)</th>
                        <th class="text-center">@Html.DisplayNameFor(m => m.PendingTransactions.First().debtId)</th>
                        <th class="text-center">@Html.DisplayNameFor(m => m.PendingTransactions.First().batchId)</th>
                        <th class="text-center">Delete</th>
                    </tr>
                </thead>
                <tbody>
                    @foreach (var transaction in Model.PendingTransactions)
                    {
                        <tr id="row_@transaction.transactionBatchId">
                            <td>
                                @Html.CheckBoxFor(m => transaction.selectedForProcessing)
                                @Html.HiddenFor(m => transaction.selectedForProcessing)
                            </td>
                            <td class="text-center">@Html.DisplayFor(m => transaction.transactionDate)
                            @Html.HiddenFor(m => transaction.transactionDate)
                            </td>
                            <td class="text-center">
                                @Html.DisplayFor(m => transaction.debtId)
                            </td>
                            <td class="text-center">@Html.DisplayFor(m => transaction.transactionBatchId)</td>
                            <td><a onclick="deleteTransaction(@transaction.transactionBatchId)" href="#"><i class="bi bi-trash" aria-hidden="true"></i></a>
                            </td>
                        </tr>
                    }
                </tbody>
            </table>

            <div class="row">
                <div class="col-md-12">
                    <input type="submit" class="btn btn-success float-right" />
                </div>
            </div>
        }
    </div>
</div>

For this model:

public class PendingTransactionsViewModel
{
    public List<PendingTransactionViewModel> PendingTransactions { get; set; } = new();
}

public class PendingTransactionViewModel
{
    public int? transactionBatchId { get; set; }
    public int? debtId { get; set; }
    public DateTime? transactionDate { get; set; }
    public bool selectedForProcessing { get; set; } = false;
}

While I'm able to manipulate the checkbox on the view, when I postback the model property PendingTransactions is not posting back to the controller. What am I missing in order to get the list back in the controller?

Serge
  • 40,935
  • 4
  • 18
  • 45
Laziale
  • 7,965
  • 46
  • 146
  • 262
  • You have both the ASP.NET MVC (on the full, classic .NET framework), as well as ASP.NET **Core** MVC - which one are you really using - or do you really need support for both?? – marc_s Jun 18 '21 at 15:38
  • This is .net core mvc – Laziale Jun 18 '21 at 15:41
  • Does this answer your question? [Correct, idiomatic way to use custom editor templates with IEnumerable models in ASP.NET MVC](https://stackoverflow.com/questions/25333332/correct-idiomatic-way-to-use-custom-editor-templates-with-ienumerable-models-in) – GSerg Jun 18 '21 at 15:41
  • @Laziale Don't let it confuse you that you are not using custom templates like in the dupe. The underlying issue is the same, `foreach (var transaction in Model.PendingTransactions)` and `m => transaction.*` is what brakes it. The solution is also the same. – GSerg Jun 18 '21 at 15:42

1 Answers1

0

try to replace foreach loop by for loop

@for (var i=0; i< Model.PendingTransactions.Count; i+=1)
                    {
  <tr id="row_@Model.PendingTransactions[i].transactionBatchId">
   <td>
  @Html.CheckBoxFor(m => m.PendingTransactions[i].selectedForProcessing)
@Html.HiddenFor(m => m.PendingTransactions[i].selectedForProcessing)
  </td>
   <td class="text-center">
     @Html.DisplayFor(m => m.PendingTransactions[i].transactionDate)
    @Html.HiddenFor(m => m.PendingTransactions[i].transactionDate)
    </td>
.....
}
Serge
  • 40,935
  • 4
  • 18
  • 45