0

I use Visual Studio 2013 for a MVC website written in C# using JQuery.

I have two classes:

public class DetailModel
{
   public bool  isSelected { get; set; }
   public int[] forSelect { get; set; }
}

public class MainModel
{
   public DetailModel[] details { get; set; }
}

Main class includes an array of detail classes

I pass a MainModel to the View and in the view I have a table, every row of that table has two columns, first column has a checkbox and second column has a select. When I submit the form the controller must receive an array of structures with a) the checkbox b) an array of values selected in the SELECT but receives an array with the ONLY values of the first row.

I can't find a way to write correctly the view. I think is a bind problem.

@model MainModel

@foreach (DetailModel det in Model.details )
{
    <input type="checkbox" name="???" value="@soa.id" />                    
    <select multiple="multiple" name="???" >
}

In place of "???" (name) what should I write?

This is how the page as appears: enter image description here


Edit 01: reading Stephen's link I made a step forward. Changing the code to this:

public class MainModel
{
   public List<DetailModel> details { get; set; }
}

@for (int i = 0; i < Model.details.Count(); i++) 
{ 
   <input type="checkbox" name="details[@i].isSelected" /> 
   <select multiple="multiple" name="details[@i].forSelect " > 
} 

now when I post the controller receives "something". Something is ALWAYS ONLY the values of first row. Nothing else: an array of structures with always only one element. Looking with F12 in the browser I see for the select names like details[0].forSelect, details1.forSelect, details[2].forSelect ...

Giorgio Forti
  • 131
  • 2
  • 14
  • 1
    Suggest you start by reading [Post an HTML Table to ADO.NET DataTable](https://stackoverflow.com/questions/30094047/post-an-html-table-to-ado-net-datatable/30094943#30094943) to understand how to bind to collections –  Aug 22 '18 at 09:57
  • Hi Stephen, reading your link I made a step forward. Changing the code to this: @for (int i = 0; i < Model.details.Count(); i++) { – Giorgio Forti Aug 22 '18 at 13:46
  • Inside you `for` loop, its `@Html.CheckBoxFor(m => m.details[i].isSelected)` (and for the ` –  Aug 22 '18 at 22:02
  • Stephen, if you come to Italy, northern Italy, I owe you a beer and the best pizza you'll ever eat ... the great difference was the CheckBoxFor. Changing from the manually created to CheckBoxFor magically all works. Unfortunately I don't understand WHY now all works ... the two HTML lines seems the same. Really thank you for your help and your patience. – Giorgio Forti Aug 23 '18 at 09:40
  • As a said, look at the difference in the html that is generated - `CheckBoxFor()` generates 2 inputs with the correct name attribute. One with `value="True"` and one with `value="False" for binding to a `bool` - if is checked, your `isSelected` will be `true`, and if not, the value will be `false` because its bound to the hidden input (unchecked checkboxes do not submit a value) –  Aug 23 '18 at 09:44
  • Ahhhhhh, right, my "hand made" code had only the first line, not the second line with "false". Ok now it works AND I know why ... – Giorgio Forti Aug 23 '18 at 10:19

0 Answers0