0

I have a viewmodel consisting 2 lists:

public class RandomViewmodel
{
        public List<Model1> Model1 { get; set; }
        public List<Model2> Model2 { get; set; }
}

When I access these viewmodel to create view that works fine

@for (int i = 0; i < Model.Model1.Count(); i++)
{
    x = i;
    var item = Model.Model1.ElementAt(i);
  <tr>                 
    <td>
        @Html.Editor("[" + i + "].Amount", new { htmlAttributes = new { @class = "form-control", @Value = item.Amount } })                       
    </td>
 </tr>
}

@for (int k=0; k < Model.Model2.Count; k++)
{
 x = x + 1;
 var bacc = Model.Model2.ElementAt(k);
 @Html.Hidden("[" + k + "].ID", bacc.ID, new { htmlAttributes = new { @id = "[" + k + "].ID" } })

 <tr style="font-weight:bold;">
     <td style="padding-left: 25px;">
          @(x+1) @bacc.name
     </td>
     <td class="adder_table_right" width="130px">
         @Html.Editor("[" + k + "].MiscAmount1", new { htmlAttributes = new { @class = "form-control" } })
     </td>
     <td class="adder_table_right" width="130px">
         @Html.Editor("[" + k + "].MiscAmount2", new { htmlAttributes = new { @class = "form-control" } })
     </td>
 </tr>                    
}

Here is my controller action:

[HttpPost]

public ActionResult Index(RandomViewModel vm)

Now when I hit submit button, I can see that the viewmodel object vm receives NULL for both of the models.

adiga
  • 34,372
  • 9
  • 61
  • 83
  • Your generating `name` attributes which have no relationship at all to your model - its `@for (int i = 0; i < Model.Model1.Count; i++) @Html.EditorFor(m => m.Model1[i]Amount) }` and similar for the 2nd loop. –  Feb 03 '17 at 10:40
  • And refer [this answer](http://stackoverflow.com/questions/30094047/html-table-to-ado-net-datatable/30094943#30094943) to understand how to bind to collections –  Feb 03 '17 at 10:41
  • The name attribute belongs to the second model of my viewmodel public List Model2 { get; set; } moreover, this data is coming fine in my view. But when submitting value the post action receives null for the viewmodel – Arindam Ghosh Feb 03 '17 at 10:44
  • I guess you did not bother to read the link! Or try the correct code in my comment. –  Feb 03 '17 at 10:44
  • Thanks for your help. It worked out. Really appreciate ;) – Arindam Ghosh Feb 06 '17 at 11:39

3 Answers3

0

You should reference to each list in your Model.

and value Attributes will be auto-initialize when you are using Html Helpers

use this codes it should work:

@for (int i = 0; i < Model.Model1.Count(); i++)
{
x = i;
var item = Model.Model1.ElementAt(i);
<tr>                 
    <td>
        @Html.EditorFor(m => m.Model1[i].Amount, new { @class = "form-control"})                       
    </td>
</tr>
}


@for (int k=0; k < Model.Model2.Count; k++)
{
 x = x + 1;
 var bacc = Model.Model2.ElementAt(k);
 @Html.HiddenFor(m => m.Model2[i].ID)
 <tr style="font-weight:bold;">
     <td style="padding-left: 25px;">
          @(x+1) @bacc.name
     </td>
     <td class="adder_table_right" width="130px">
         @Html.EditorFor(m => m.Model2[i].MiscAmount1, new { @class = "form-control" })
     </td>
     <td class="adder_table_right" width="130px">
         @Html.EditorFor(m => m.Model2[i].MiscAmount2, new { @class = "form-control" })

     </td>
 </tr>                    
}
Ali Zeinali
  • 551
  • 4
  • 16
0

use @Html.EditorFor to resolve this issue

0

The problem was the naming convention, which got resolved after I go through the following article referred by Stephen Muecke of stackoverflow.

This is for other fellow developer, if they got stuck in the same situation like mine. Please go through this post first, it really helped a lot.

Post an HTML Table to ADO.NET DataTable

Happy coding ;)

Community
  • 1
  • 1