【问题标题】:How to pass back list of items as model from view to controller?如何将项目列表作为模型从视图传递给控制器​​?
【发布时间】: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


【解决方案1】:

您正在使用CountSpendCategories 作为索引:

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

但你实际上是在索引SpendBReakdown:

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

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

【讨论】:

  • 我怎样才能让这两个相关联?我需要每个 SpendCategories 的文本区域(有 10 个)现在 SpendBreakdowns 还没有任何内容
  • 填充模型时,您必须确保将正确的数据加载到SpendBreakdown。我必须查看执行此操作的代码才能了解那里有什么问题。
  • 使用填充模型的控制器进行了更新
  • 看起来您正在检索基于spendIdspendDetailsIdSpendBreakdown,但再次索引类别数。我认为您假设每个类别会有一个SpendBreakdown,但是当您从数据库中检索它时不会发生这种情况。您不能将for 循环更改为SpendBreakdown 上的foreach 循环吗?似乎没有理由在 SpendCategories.Count() 上编制索引。
  • 我在某处读到,为了返回所有故障,我需要使用 for 循环。我现在就试试你的建议
猜你喜欢
  • 2016-07-24
  • 1970-01-01
  • 1970-01-01
  • 2015-10-06
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多