ASP.Net MVC Array of Hidden Fields

I send List <> to the view and then turn this list into an array of hidden fields. This is done using a partial view to build a table.

<%@ Control Language="C#" Inherits="System.Web.Mvc.ViewUserControl<BudgieMoneySite.Models.TransactionSplitLine>" %>
<tr>
    <td>
        <%=Model.Category %>
        <%=Html.HiddenFor(x => x.CategoryId)%>
    </td>
    <td>
        <%=Model.SubCategory %>
        <%=Html.HiddenFor(x => x.SubCategoryId)%>
    </td>
    <td>
        <%=Model.Amount %>
        <%=Html.HiddenFor(x => x.AmountValue)%>
    </td>

</tr>

I have x number of rows .... so I hope I can get the values ​​from hidden fields as an array. I have a field in my model called public string [] CategoryIds {get; set; }

(I think the problem may be that the hidden field is called "CategoryId" and the mu model requires "CategoryIds").

On visualization, I see that I have a line that looks like this:

<tr>
<td>
    Medical
    <input id="TransactionSplitLines_2__CategoryId" name="TransactionSplitLines[2].CategoryId" type="hidden" value="6" />

</td>
<td>
    Over the Counter Medicines
    <input id="TransactionSplitLines_2__SubCategoryId" name="TransactionSplitLines[2].SubCategoryId" type="hidden" value="22" />
</td>
<td>
    111
    <input id="TransactionSplitLines_2__AmountValue" name="TransactionSplitLines[2].AmountValue" type="hidden" value="0" />
</td>

Then I try to return the data to the controller as follows:

[HttpPost]
public ActionResult AccountTransaction(AccountTransactionView model)
{
    var reply = CreateModel(model);
    if (model.CategoryIds != null)
    {
        for (int i = 0; i < model.CategoryIds.Count(); i++ )
        {
            reply.TransactionSplitLines.Add(new TransactionSplitLine
                                                {
                                                    Amount = "100", 
                                                    Category = Services.CategoryServices.GetCategoryById(int.Parse(model.CategoryIds[i])).Description, 
                                                    SubCategoryId = model.SelectedCategoryId, 
                                                    SubCategory = Services.SubCategoryServices.GetSubCategoryById(model.SelectedSubCategoryId).Description
                                                });
        }
    }
    reply.TransactionSplitLines.Add(new TransactionSplitLine
                                        {
                                            Amount = model.Amount, 
                                            Category = Services.CategoryServices.GetCategoryById(model.SelectedCategoryId).Description, 
                                            SubCategory = Services.SubCategoryServices.GetSubCategoryById(model.SelectedSubCategoryId).Description, 
                                            CategoryId = model.SelectedCategoryId,
                                            SubCategoryId = model.SelectedSubCategoryId
                                        });
    return View("AccountTransaction", reply);
}

But CategoryIds is always null ...

, "TransactionSplitLines [2].SubCategoryId" ( , ). , , ... , , ?

EDIT: AccountTransactionView.

 public class AccountTransactionView
{
  public AccountTransactionView()
  {
      TransactionSplitLines = new List<TransactionSplitLine>();
  }

    [DisplayName("Bank Account")]
    public IEnumerable<SelectListItem> Accounts { get; set; }

    [DisplayName("Cost Center")]
    public IEnumerable<SelectListItem> CostCenters { get; set; }

    [DisplayName("Payee")]
    public IEnumerable<SelectListItem> Payees { get; set; }

    [DisplayName("Transaction Type")]
    public List<TransactionTypeView> TransactionTypes { get; set; }

    public int SelectedAccountId { get; set; }
    public int SelectedCostCenterId { get; set; }
    public int SelectedPayeeId { get; set; }
    [DisplayName("Category")]
    public int SelectedCategoryId { get; set; }
    [DisplayName("Sub Category")]
    public int SelectedSubCategoryId { get; set; }
    public int SelectedTransactionTypeId { get; set; }
    [DisplayName("Budget")]
    public int SelectedBudgetId { get; set; }

    [DisplayName("Transaction Date")]
    public DateTime TransactionDate { get; set; }
    [DisplayName("Transaction Date")]
    public string FormattedDateTime
    {
        get
        {
            return TransactionDate.ToString("dd/MM/yyyy");

        }
    }
    public int TransactionId { get; set; }
    public bool Deleted { get; set; }
    public List<TransactionSplitLine> TransactionSplitLines { get; set; }
    public string Amount { get; set; }

}
+3
2

, :

@if (Model.Items != null)
{
    for (int i = 0; i < Model.Items.Length; i++)
    {
        @Html.HiddenFor(x => x.Items[i])
    }
}
+3

Source: https://habr.com/ru/post/1793180/


All Articles