2016-07-25 10 views
-1

私はモデルを持っています(下を参照)。ユーザーは単一の支出の詳細を見ることができるはずです(すでにajaxで作業中で、ドロップダウン駆動です)。支出の詳細が読み込まれたら、その詳細の合計金額を入力するか、カテゴリ別に分割することができます(別々のモデル(SpendCategories)からのものです)。私は最初にそれらを元に戻すためにカテゴリを繰り返し、SpendCategoryNameをSpendBreakdown.csのSpendCategoryにマップします。 SpendCategory名ごとにテキストエリアを繰り返して生成し、モデル全体(予算)を返送しようとしています。私はこれを実行すると、それはループの中HiddenForライン上の範囲execeptionのうち、私に引数を与えビューからコントローラへのアイテムのリストをモデルとして戻すにはどうすればいいですか?

問題。私はちょうどポストに金額とカテゴリを保存する方法が必要です。残念ながら、私はMVCを初めとしており、モデルの設定方法を変更することはできません。この作品以外はすべて動作しています。

私は

public int SpendId {get; set;} 
public List<SpendCategories> SpendCategories {get; set;} 

Spend.csプライバシーの目的のために、このの一部を下にスクラブしなければならなかったが、私に知らせてください、あなたが明確化が必要な場合

モデル

public class Budgets : Financials 
{ 
    public Spend Spending { get; set; } 
    public SpendDetails SpendingDetails { get; set; } 
    public List<SpendBreakdown> SpendingBreakdown{ get; set; } 
} 

SpendCategories.cs(この部分的であるとしている呼んでいるメインページのPRE人口DATA)

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

コントローラ(ドロップダウンリストの変更あなたは、インデックスのためにSpendCategoriesCountを使用している

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ループの下の最初の行の範囲外の例外を取得しています。私はこの前に1つのカテゴリで公のList SpendingBreakdownがリストされた前にそれを試してみました。しかし今、SpendingBreakdownは私が問題を抱えているリストです – cxwilson

答えて

0

のモデル移入するために呼び出されます:

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

しかし、あなたが実際にSpendBReakdownのインデックスを作成されています

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

インデックスがSpendBreakdownの終わりを超えて起こっているように、これらの2つの項目は、項目の数が同じではありません。相関関係があると思われる場合は、それらが正しく入力されていることを確認してください。それ以外の場合はSpendBreakdownsforeachと入力してください。

+0

どうすればこれらの2つを相関させることができますか?私は各SpendCategoriesのテキストエリアが必要です(10があります)。今すぐSpendBreakdownsには何もありません。 – cxwilson

+0

あなたのモデルを作成するときに、正しいデータが 'SpendBreakdown'にロードされていることを確認する必要があります。私は何が間違っているかを見るためにそれを行うコードを見なければならないでしょう。 –

+0

は、モデルを投入しているコントローラで更新されました – cxwilson

関連する問題