I need to handle a List<List<T>> in a form and couldn't find how to proceed with old version of MVC and .NET.
I did try the following:
Model:
public class MyModel
{
public List<List<Item>> ListA { get; set; }
}
public class Item
{
public string Name { get; set; }
public string Spec { get; set; }
public string Type { get; set; }
public string Field1 { get; set; }
public string Field2 { get; set; }
public string Field3 { get; set; }
public string Field4 { get; set; }
public string Field5 { get; set; }
}
View:
<%@ Control Language="C#" Inherits="System.Web.Mvc.ViewUserControl<MyModel>" %>
<% for(int i = 0; i < Model.ListA.Count; i++) { %>
<fieldset>
<div>
<input disabled="disabled" value="Name" />
<input disabled="disabled" value="Spec" />
<input disabled="disabled" value="Type" />
<input disabled="disabled" value="Field 1" />
<input disabled="disabled" value="Field 2" />
<input disabled="disabled" value="Field 3" />
<input disabled="disabled" value="Field 4" />
<input disabled="disabled" value="Field 5" />
</div>
<% for(int j = 0; j < Model.ListA[i].Count; j++) { %>
<div>
<%= Html.EditorFor(m => m.ListA[i][j].Name)%>
<%= Html.EditorFor(m => m.ListA[i][j].Spec)%>
<%= Html.EditorFor(m => m.ListA[i][j].Type)%>
<%= Html.EditorFor(m => m.ListA[i][j].Field1)%>
<%= Html.EditorFor(m => m.ListA[i][j].Field2)%>
<%= Html.EditorFor(m => m.ListA[i][j].Field3)%>
<%= Html.EditorFor(m => m.ListA[i][j].Field4)%>
<%= Html.EditorFor(m => m.ListA[i][j].Field5)%>
</div>
<% } %>
</div>
<% } %>
Controller:
[HttpGet]
public ActionResult MyAction(int Id)
{
// MyModel modObj = MyService.GetModelById(Id);
MyModel modObj = new MyModel
{
ListA = new List<List<Item>>
{
new List<Item>
{
new Item { Name = "0 - 0" },
new Item { Name = "0 - 1" },
new Item { Name = "0 - 2" }
},
new List<Item>
{
new Item { Name = "1 - 0" },
new Item { Name = "1 - 1" },
new Item { Name = "1 - 2" }
}
}
}
return View(modObj);
}
[HttpPost]
public ActionResult MyAction(MyModel model)
{
// anything
return null;
}
My problem is that currently, even if I can correctly see the editors for every item (here the two lists of three items), when I post it back, I only see the first list of three items in the controller.
I also tried to change my model to:
public class MyModel
{
public List<ItemList> ListA { get; set; }
}
public class ItemList
{
public string PropId { get; set; }
public List<Item> ListB { get; set; }
}
public class Item
{
public string Name { get; set; }
public string Spec { get; set; }
public string Type { get; set; }
public string Field1 { get; set; }
public string Field2 { get; set; }
public string Field3 { get; set; }
public string Field4 { get; set; }
public string Field5 { get; set; }
}
and changed my View and Controller accordingly, but I still get the same result: the first List level only contains one List when I post it back - I only get "0 - 0", "0 - 1" & "0 - 2" when setting a breakpoint in the POST method of the controller.
I'm out of idea here.
Edit: To answer Jonny's question, this is how it looks like with List<ItemList>:
<fieldset>
<div>
<input disabled="disabled" value="Name" />
<input disabled="disabled" value="Spec" />
<input disabled="disabled" value="Type" />
<input disabled="disabled" value="Field 1" />
<input disabled="disabled" value="Field 2" />
<input disabled="disabled" value="Field 3" />
<input disabled="disabled" value="Field 4" />
<input disabled="disabled" value="Field 5" />
</div>
<div>
<input name="ListA[0].ListB[0].Name" id="ListA_0__ListB_0__Name" type="text" value="0 - 0"></input>
<input name="ListA[0].ListB[0].Spec" id="ListA_0__ListB_0__Spec" type="text"></input>
<input name="ListA[0].ListB[0].Type" id="ListA_0__ListB_0__Type" type="text"></input>
<input name="ListA[0].ListB[0].Field1" id="ListA_0__ListB_0__Field1" type="text"></input>
<input name="ListA[0].ListB[0].Field2" id="ListA_0__ListB_0__Field2" type="text"></input>
<input name="ListA[0].ListB[0].Field3" id="ListA_0__ListB_0__Field3" type="text"></input>
<input name="ListA[0].ListB[0].Field4" id="ListA_0__ListB_0__Field4" type="text"></input>
<input name="ListA[0].ListB[0].Field5" id="ListA_0__ListB_0__Field5" type="text"></input>
</div>
<div>
<input name="ListA[0].ListB[1].Name" id="ListA_0__ListB_1__Name" type="text" value="0 - 1"></input>
<input name="ListA[0].ListB[1].Spec" id="ListA_0__ListB_1__Spec" type="text"></input>
<input name="ListA[0].ListB[1].Type" id="ListA_0__ListB_1__Type" type="text"></input>
<input name="ListA[0].ListB[1].Field1" id="ListA_0__ListB_1__Field1" type="text"></input>
<input name="ListA[0].ListB[1].Field2" id="ListA_0__ListB_1__Field2" type="text"></input>
<input name="ListA[0].ListB[1].Field3" id="ListA_0__ListB_1__Field3" type="text"></input>
<input name="ListA[0].ListB[1].Field4" id="ListA_0__ListB_1__Field4" type="text"></input>
<input name="ListA[0].ListB[1].Field5" id="ListA_0__ListB_1__Field5" type="text"></input>
</div>
<div>
<input name="ListA[0].ListB[2].Name" id="ListA_0__ListB_2__Name" type="text" value="0 - 2"></input>
<input name="ListA[0].ListB[2].Spec" id="ListA_0__ListB_2__Spec" type="text"></input>
<input name="ListA[0].ListB[2].Type" id="ListA_0__ListB_2__Type" type="text"></input>
<input name="ListA[0].ListB[2].Field1" id="ListA_0__ListB_2__Field1" type="text"></input>
<input name="ListA[0].ListB[2].Field2" id="ListA_0__ListB_2__Field2" type="text"></input>
<input name="ListA[0].ListB[2].Field3" id="ListA_0__ListB_2__Field3" type="text"></input>
<input name="ListA[0].ListB[2].Field4" id="ListA_0__ListB_2__Field4" type="text"></input>
<input name="ListA[0].ListB[2].Field5" id="ListA_0__ListB_2__Field5" type="text"></input>
</div>
</fieldset>
<fieldset>
<div>
<input disabled="disabled" value="Name" />
<input disabled="disabled" value="Spec" />
<input disabled="disabled" value="Type" />
<input disabled="disabled" value="Field 1" />
<input disabled="disabled" value="Field 2" />
<input disabled="disabled" value="Field 3" />
<input disabled="disabled" value="Field 4" />
<input disabled="disabled" value="Field 5" />
</div>
<div>
<input name="ListA[1].ListB[0].Name" id="ListA_1__ListB_0__Name" type="text" value="1 - 0"></input>
<input name="ListA[1].ListB[0].Spec" id="ListA_1__ListB_0__Spec" type="text"></input>
<input name="ListA[1].ListB[0].Type" id="ListA_1__ListB_0__Type" type="text"></input>
<input name="ListA[1].ListB[0].Field1" id="ListA_1__ListB_0__Field1" type="text"></input>
<input name="ListA[1].ListB[0].Field2" id="ListA_1__ListB_0__Field2" type="text"></input>
<input name="ListA[1].ListB[0].Field3" id="ListA_1__ListB_0__Field3" type="text"></input>
<input name="ListA[1].ListB[0].Field4" id="ListA_1__ListB_0__Field4" type="text"></input>
<input name="ListA[1].ListB[0].Field5" id="ListA_1__ListB_0__Field5" type="text"></input>
</div>
<div>
<input name="ListA[1].ListB[1].Name" id="ListA_1__ListB_1__Name" type="text" value="1 - 1"></input>
<input name="ListA[1].ListB[1].Spec" id="ListA_1__ListB_1__Spec" type="text"></input>
<input name="ListA[1].ListB[1].Type" id="ListA_1__ListB_1__Type" type="text"></input>
<input name="ListA[1].ListB[1].Field1" id="ListA_1__ListB_1__Field1" type="text"></input>
<input name="ListA[1].ListB[1].Field2" id="ListA_1__ListB_1__Field2" type="text"></input>
<input name="ListA[1].ListB[1].Field3" id="ListA_1__ListB_1__Field3" type="text"></input>
<input name="ListA[1].ListB[1].Field4" id="ListA_1__ListB_1__Field4" type="text"></input>
<input name="ListA[1].ListB[1].Field5" id="ListA_1__ListB_1__Field5" type="text"></input>
</div>
<div>
<input name="ListA[1].ListB[2].Name" id="ListA_1__ListB_2__Name" type="text" value="1 - 2"></input>
<input name="ListA[1].ListB[2].Spec" id="ListA_1__ListB_2__Spec" type="text"></input>
<input name="ListA[1].ListB[2].Type" id="ListA_1__ListB_2__Type" type="text"></input>
<input name="ListA[1].ListB[2].Field1" id="ListA_1__ListB_2__Field1" type="text"></input>
<input name="ListA[1].ListB[2].Field2" id="ListA_1__ListB_2__Field2" type="text"></input>
<input name="ListA[1].ListB[2].Field3" id="ListA_1__ListB_2__Field3" type="text"></input>
<input name="ListA[1].ListB[2].Field4" id="ListA_1__ListB_2__Field4" type="text"></input>
<input name="ListA[1].ListB[2].Field5" id="ListA_1__ListB_2__Field5" type="text"></input>
</div>
</fieldset>
and with List<List<Item>>:
<fieldset>
<div>
<input disabled="disabled" value="Name" />
<input disabled="disabled" value="Spec" />
<input disabled="disabled" value="Type" />
<input disabled="disabled" value="Field 1" />
<input disabled="disabled" value="Field 2" />
<input disabled="disabled" value="Field 3" />
<input disabled="disabled" value="Field 4" />
<input disabled="disabled" value="Field 5" />
</div>
<div>
<input name="ListA[0][0].Name" id="ListA_0__0__Name" type="text" value="0 - 0"></input>
<input name="ListA[0][0].Spec" id="ListA_0__0__Spec" type="text"></input>
<input name="ListA[0][0].Type" id="ListA_0__0__Type" type="text"></input>
<input name="ListA[0][0].Field1" id="ListA_0__0__Field1" type="text"></input>
<input name="ListA[0][0].Field2" id="ListA_0__0__Field2" type="text"></input>
<input name="ListA[0][0].Field3" id="ListA_0__0__Field3" type="text"></input>
<input name="ListA[0][0].Field4" id="ListA_0__0__Field4" type="text"></input>
<input name="ListA[0][0].Field5" id="ListA_0__0__Field5" type="text"></input>
</div>
<div>
<input name="ListA[0][1].Name" id="ListA_0__1__Name" type="text" value="0 - 1"></input>
<input name="ListA[0][1].Spec" id="ListA_0__1__Spec" type="text"></input>
<input name="ListA[0][1].Type" id="ListA_0__1__Type" type="text"></input>
<input name="ListA[0][1].Field1" id="ListA_0__1__Field1" type="text"></input>
<input name="ListA[0][1].Field2" id="ListA_0__1__Field2" type="text"></input>
<input name="ListA[0][1].Field3" id="ListA_0__1__Field3" type="text"></input>
<input name="ListA[0][1].Field4" id="ListA_0__1__Field4" type="text"></input>
<input name="ListA[0][1].Field5" id="ListA_0__1__Field5" type="text"></input>
</div>
<div>
<input name="ListA[0][2].Name" id="ListA_0__2__Name" type="text" value="0 - 2"></input>
<input name="ListA[0][2].Spec" id="ListA_0__2__Spec" type="text"></input>
<input name="ListA[0][2].Type" id="ListA_0__2__Type" type="text"></input>
<input name="ListA[0][2].Field1" id="ListA_0__2__Field1" type="text"></input>
<input name="ListA[0][2].Field2" id="ListA_0__2__Field2" type="text"></input>
<input name="ListA[0][2].Field3" id="ListA_0__2__Field3" type="text"></input>
<input name="ListA[0][2].Field4" id="ListA_0__2__Field4" type="text"></input>
<input name="ListA[0][2].Field5" id="ListA_0__2__Field5" type="text"></input>
</div>
</fieldset>
<fieldset>
<div>
<input disabled="disabled" value="Name" />
<input disabled="disabled" value="Spec" />
<input disabled="disabled" value="Type" />
<input disabled="disabled" value="Field 1" />
<input disabled="disabled" value="Field 2" />
<input disabled="disabled" value="Field 3" />
<input disabled="disabled" value="Field 4" />
<input disabled="disabled" value="Field 5" />
</div>
<div>
<input name="ListA[1][0].Name" id="ListA_1__0__Name" type="text" value="1 - 0"></input>
<input name="ListA[1][0].Spec" id="ListA_1__0__Spec" type="text"></input>
<input name="ListA[1][0].Type" id="ListA_1__0__Type" type="text"></input>
<input name="ListA[1][0].Field1" id="ListA_1__0__Field1" type="text"></input>
<input name="ListA[1][0].Field2" id="ListA_1__0__Field2" type="text"></input>
<input name="ListA[1][0].Field3" id="ListA_1__0__Field3" type="text"></input>
<input name="ListA[1][0].Field4" id="ListA_1__0__Field4" type="text"></input>
<input name="ListA[1][0].Field5" id="ListA_1__0__Field5" type="text"></input>
</div>
<div>
<input name="ListA[1][1].Name" id="ListA_1__1__Name" type="text" value="1 - 1"></input>
<input name="ListA[1][1].Spec" id="ListA_1__1__Spec" type="text"></input>
<input name="ListA[1][1].Type" id="ListA_1__1__Type" type="text"></input>
<input name="ListA[1][1].Field1" id="ListA_1__1__Field1" type="text"></input>
<input name="ListA[1][1].Field2" id="ListA_1__1__Field2" type="text"></input>
<input name="ListA[1][1].Field3" id="ListA_1__1__Field3" type="text"></input>
<input name="ListA[1][1].Field4" id="ListA_1__1__Field4" type="text"></input>
<input name="ListA[1][1].Field5" id="ListA_1__1__Field5" type="text"></input>
</div>
<div>
<input name="ListA[1][2].Name" id="ListA_1__2__Name" type="text" value="1 - 2"></input>
<input name="ListA[1][2].Spec" id="ListA_1__2__Spec" type="text"></input>
<input name="ListA[1][2].Type" id="ListA_1__2__Type" type="text"></input>
<input name="ListA[1][2].Field1" id="ListA_1__2__Field1" type="text"></input>
<input name="ListA[1][2].Field2" id="ListA_1__2__Field2" type="text"></input>
<input name="ListA[1][2].Field3" id="ListA_1__2__Field3" type="text"></input>
<input name="ListA[1][2].Field4" id="ListA_1__2__Field4" type="text"></input>
<input name="ListA[1][2].Field5" id="ListA_1__2__Field5" type="text"></input>
</div>
</fieldset>
Edit 2
I finally managed to make it, rewriting the same code line by line...
So I have no idea why it didn't work before.
//View