c# - How to pass back list of items as model from view to controller? -


i have model (see below) , user should able @ single spend details (already working ajax , dropdown driven). once spend details loaded should able enter in either total amount details, or break down category (which coming different model (spendcategories) prepopulated). first iterate through categories pull them , each row map spendcategoryname spendcategory in spendbreakdown.cs. trying iterate , generate textarea per spendcategory name , send whole model (budgets).

problem

when run gives me argument out of range exeception on hiddenfor line in loop. need way save amounts , categories on post back. unfortunately new mvc , cannot change way model set up. working except piece.

i had scrub of down privacy purposes please let me know if need clarification

model

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 (pre populated 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;} 

partial view on main page calling partial has submit form

    <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 = 0; < 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

controller (called populate model on dropdownlist change

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

you using count of spendcategories index:

@for (int = 0; < model.spend.spendcategories.count(); i++) 

but indexing spendbreakdown:

@html.hiddenfor(model => model.spendbreakdown[i].spendcategory, new { @value = @cat.spendcategorydescription }) 

these 2 items not have same number of items, index going beyond end of spendbreakdown. make sure populated correctly if supposed correlate. otherwise, use foreach on spendbreakdowns.


Comments

Popular posts from this blog

jOOQ update returning clause with Oracle -

java - Warning equals/hashCode on @Data annotation lombok with inheritance -

java - BasicPathUsageException: Cannot join to attribute of basic type -