2016-07-25 49 views
-1

我有一個模型(見下文),用戶應該可以查看單個支出細節(已經使用ajax並且是下拉驅動的)。一旦花費細節被加載,它們應該能夠輸入該細節的總金額,或者按類別(來自預填充的不同模型(SpendCategories))進行分類。我首先遍歷類別以將它們拉回,並將SpendCategoryName映射到SpendBreakdown.cs中的SpendCategory的每一行。我正在嘗試迭代併爲每個SpendCategory名稱生成textarea,然後發回整個模型(預算)。如何將物品列表作爲模型從視圖傳遞迴控制器?

問題

當我運行這個它給了我一個參數超出範圍exeception對在該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); 
    } 
+0

您的控制器操作方法是否接收模型?或者是什麼? –

+0

@devlincarnate目前沒有,我正在FOR循環下的第一行中獲得第一個超出範圍異常的參數。當我在公開List SpendingBreakdown是一個列表之前以1個類別嘗試了這個時,它使所有事情都恢復得很好。但現在SpendingBreakdown是我遇到的問題列表 – cxwilson

回答

0

您正在使用的SpendCategoriesCount索引的模式:

@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即可。

+0

我怎樣才能讓這兩個關聯?我需要每個SpendCategories的textareas(現在有10個)現在SpendBreakdowns沒有任何內容 – cxwilson

+0

當您填充模型時,您必須確保將正確的數據加載到「SpendBreakdown」中。我將不得不看看那些能夠看到哪裏出錯的代碼。 –

+0

用控制器填充模型更新 – cxwilson

相關問題