x.CategoryId)%> -6ren">
gpt4 book ai didi

asp.net-mvc - ASP.Net MVC 隐藏字段数组

转载 作者:行者123 更新时间:2023-12-01 23:08:56 26 4
gpt4 key购买 nike

我将 List<> 发送到 View,然后将该列表转换为隐藏字段数组。这是使用局部 View 来构建表格的。

<%@ 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>

我有 x 行......所以我希望我可以从隐藏字段中获取值作为数组。我的模型中有一个字段,定义为 public string[] CategoryIds { get;放; }

(我认为问题可能在于被称为“CategoryId”的隐藏字段和想要“CategoryIds”的 mu 模型)。

渲染时,我看到我有一行如下所示:

<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>

然后我尝试像这样在 Controller 中取回数据:

[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);
}

但 CategoryIds 始终为空...

当隐藏字段的名称为“TransactionSplitLines[2].SubCategoryId”(我认为这是第二行)时,如何引用隐藏字段。我想我有一个命名问题,只是有点纠结……但希望你能帮忙?

编辑:这是 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; }

}

最佳答案

关于asp.net-mvc - ASP.Net MVC 隐藏字段数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5048055/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com