如何将项目列表作为模型从视图传递给控制器​​?

时间:2016-07-25 15:51:04

标签: c# asp.net-mvc asp.net-mvc-4 entity-framework-5

我有一个模型(见下文),用户应该能够查看单个支出详细信息(已经使用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>

UI enter image description here

控制器(调用以在下拉列表中填充模型更改

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

1 个答案:

答案 0 :(得分:0)

您使用Count SpendCategories作为索引:

@for (int i = 0; i < Model.Spend.SpendCategories.Count(); i++)

但您实际上正在为SpendBReakdown编制索引:

@Html.HiddenFor(model => model.SpendBreakdown[i].SpendCategory, new { @Value = @cat.SpendCategoryDescription })

这两个项目的项目数量不同,因此索引超出SpendBreakdown的末尾。如果它们应该相关,请确保这些填充正确。否则,只需在foreach上使用SpendBreakdowns