manoj singh

manoj singh

  • 1.6k
  • 142
  • 9.8k

How To Edit Nested List in Mvc

Aug 17 2018 5:01 AM
 Problem - List binding gets null
 
 
 
 
 
My View
  1. @model List<WebApplication5.Models.Site>  
  2. @{  
  3.     ViewBag.Title = "Home Page";  
  4.     int i = 0;  
  5.     int j = 0;  
  6. }  
  7. @using (Html.BeginForm("Index""Home", FormMethod.Post))  
  8. {  
  9.     <table class="table">  
  10.   
  11.         @foreach (var item in Model)  
  12.         {  
  13.             <tr>  
  14.                 <td>  
  15.                     @Html.Hidden("ItemSiteCosting.Index", (@i + 10))  
  16.                     @Html.TextBox("ItemSiteCosting[" + (@i + 10) + "].name", Model[i].name)  
  17.                     @item.name  
  18.                 </td>  
  19.                 <td>  
  20.                     <table class="table">  
  21.                         @{ j = 0;}  
  22.                         @foreach (var item2 in item.xyz.costing.Sitecosting)  
  23.                         {  
  24.   
  25.                             <tr>  
  26.                                 <td>  
  27.                                     @Html.TextBox("ItemSiteCosting[" + (@i + 10) + "].xyz.costing.Sitecosting[" + (@j + 10) + "].Amount", Model[i].xyz.costing.Sitecosting[j].Amount)  
  28.                                     @item2.Amount  
  29.   
  30.                                 </td>  
  31.                             </tr>  
  32.                             j = j + 1;  
  33.                         }  
  34.                     </table>  
  35.                 </td>  
  36.             </tr>  
  37.                                 i = i + 1;  
  38.                             }  
  39.   
  40.     </table>  
  41.     <input type="submit" value="Submit" />  
  42.                             }  
 
 
 
My models
  1. public class Site  
  2.    {  
  3.        public string name { get; set; }  
  4.        public BestPO xyz { get; set; }  
  5.    }  
  6.   
  7.    public class BestPO  
  8.    {  
  9.        public BestPO()  
  10.        {  
  11.            costing = new SiteCost();  
  12.        }  
  13.        public string Cost { get; set; }  
  14.        public SiteCost costing { get; set; }  
  15.   
  16.    }  
  17.   
  18.    public class SiteCost  
  19.    {  
  20.        public SiteCost()  
  21.        {  
  22.            Sitecosting = new List<Money>();  
  23.        }  
  24.        public string Name { get; set; }  
  25.        public List<Money> Sitecosting {get;set;}  
  26.   
  27.    }  
  28.   
  29.    public class Money  
  30.    {  
  31.        public decimal? Amount { get; set; }  
  32.   
  33.  }  

Answers (1)