【发布时间】:2016-07-25 15:51:03
【问题描述】:
我有一个模型(见下文),用户应该能够查看单个支出的详细信息(已经使用 ajax 并且是下拉驱动的)。加载支出详细信息后,他们应该能够输入该详细信息的总金额,或按类别细分(来自预填充的不同模型(SpendCategories))。我首先遍历类别以将它们拉回,然后对于每一行,我将 SpendCategoryName 映射到 SpendBreakdown.cs 中的 SpendCategory。我正在尝试迭代并生成每个 SpendCategory 名称的 textarea,然后发回整个模型(预算)。
问题
当我运行它时,它会在 for 循环中的 HiddenFor 行上给我一个参数超出范围异常。我只需要一种方法来保存回发的金额和类别。不幸的是,我是 MVC 的新手,无法更改模型的设置方式。一切正常,除了这件作品。
出于隐私目的,我不得不删除其中的一些内容,但如果您需要澄清,请告诉我
型号
public class Budgets : Financials
{
public Spend Spending { get; set; }
public SpendDetails SpendingDetails { get; set; }
public List<SpendBreakdown> SpendingBreakdown{ get; set; }
}
Spend.cs
public int SpendId {get; set;}
public List<SpendCategories> SpendCategories {get; set;}
SpendCategories.cs(预填充数据)
public int SpendCategoryId {get; set;}
public string SpendCategoryName {get; set;}
public string SpendCategoryDescription {get; set;}
SpendDetails.cs
public int SpendDetailsId {get; set;}
public int SpendId {get; set;}
public string SpendDetailName {get; set;}
public int SpendBreakdownId {get; set;}
public decimal TotalSpendAmount {get; set;}
SpendBreakdown.cs
public int SpendBreakdownId {get; set;}
public int SpendDetailsId {get; set;}
public decimal SpendBreakdownAmount {get; set;}
public string SpendCategory {get; set;}
部分视图在调用此部分的主页上有提交表单
<div class="row col-md-5">
<div class="table-responsive">
<table class="table-bordered">
<thead>
<tr>
<th class="dt-head-center">Category:</th>
<th>Total Amount: </th>
</tr>
</thead>
<tbody>
<tr>
<td>@Html.DisplayFor(model => model.SpendDetails.SpendCategories)</td>
<td>@Html.TextAreaFor(model => model.SpendDetails.TotalSpendAmount)</td>
</tr>
</tbody>
</table>
</div>
</div>
<div class="row totals">
<div class="table-responsive col-md-6">
<table class="table table-bordered table-condensed table-responsive" id="tblSpendSummary" summary="Spend Summary" style="padding-left: 10px;">
<thead>
<tr class="summary-header">
<th class="dt-head-center">Spend Category Name</th>
<th class="dt-head-center">Spend Breakdown Amount</th>
</tr>
</thead>
<tbody>
@foreach (var cat in Model.Spend.SpendCategories)
{
<tr>
<td>@cat.SpendCategoryName- @cat.SpendCategoryDescription</td>
@for (int i = 0; i < Model.Spend.SpendCategories.Count(); i++)
{
@Html.HiddenFor(model => model.SpendBreakdown[i].SpendCategory, new { @Value = @cat.SpendCategoryDescription })
@*@Html.HiddenFor(model => model.SpendBreakdown[i].SpendBreakdownId)*@
<td>@Html.TextAreaFor(model => model.SpendBreakdown[i].SpendBreakdownAmount)</td>
}
</tr>
}
</tbody>
</table>
</div>
</div>
控制器(调用以在下拉列表更改时填充模型
public ActionResult BudgetTotalAmount(int spendDetailsId)
{
var SpendId = _executionRepository.RetrieveSpendIdBySpendDetailsId (spendDetailsId).SpendId;
var model = new Budgets
{
Spending = _executionRepository.RetrieveSpendIdBySpendDetailsId(spendDetailsId),
SpendingDetails = _executionRepository.RetrieveSpendSpendDetailsBySpendDetailsId(spendDetailsId),
SpendingBreakdown = _executionRepository.RetrieveSpendBreakdown(spendId, spendDetailsId)
};
return PartialView("Budgets/_SpendBudgetsTotalAmounts", model);
}
【问题讨论】:
-
你的控制器动作方法接收模型了吗?还是什么?
-
@devlincarnate 目前没有,我在 FOR 循环下的第一行的第一行得到一个参数超出范围异常。当我之前在 public List
SpendingBreakdown 是一个列表之前尝试使用 1 个类别时,它让一切恢复得很好。但是现在 SpendingBreakdown 是我遇到问题的列表
标签: c# asp.net-mvc asp.net-mvc-4 entity-framework-5