How can I post nested data via a form submission into an ActionResult parameter? I've been attempting to implement a solution similar to the answer to this question but am not having much luck.
The section of the form in question is structured as follows:
<form method="post" action="/Product/Edit" class="tabs">
<input name="Prices[1].Price" id="Prices_1__Price" value="9.99" type="text">
<input name="Prices[1].Size2Price" id="Prices_1__Size2Price" value="0.00" type="text">
<input name="Prices[1].Size3Price" id="Prices_1__Size3Price" value="0.00" type="text">
<input name="Prices[2].Price" id="Prices_2__Price" value="5.00" type="text">
<input name="Prices[2].Size2Price" id="Prices_2__Size2Price" value="0.00" type="text">
<input name="Prices[2].Size3Price" id="Prices_2__Size3Price" value="0.00" type="text">
<input name="Prices[3].Price" id="Prices_3__Price" value="0.00" type="text">
<input name="Prices[3].Size2Price" id="Prices_3__Size2Price" value="0.00" type="text">
<input name="Prices[3].Size3Price" id="Prices_3__Size3Price" value="0.00" type="text">
<button type="submit">submit</button>
</form>
This is coming through in the log fine, with the posted data displaying as expected:
Form Submission Event: (BaseController:57-ish):
{
...
"Prices[1].Price": "9.9900",
"Prices[1].Size2Price": "0.0000",
"Prices[1].Size3Price": "0.0000",
"Prices[2].Price": "5.5800",
"Prices[2].Size2Price": "0.0000",
"Prices[2].Size3Price": "0.0000",
"Prices[3].Price": "0.0000",
"Prices[3].Size2Price": "0.0000",
"Prices[3].Size3Price": "0.0000",
...
}
In the Model, I've been trying variations to try to catch the collection of prices, the current attempt is as follows:
[DataContract]
public class EditedProductEntry
{
[DataContract]
public struct PriceCollection
{
[DataMember( Name = "Price" )]
public string Price { get; set; }
[DataMember( Name = "Size2Price" )]
public string Size2Price { get; set; }
[DataMember( Name = "Size3Price" )]
public string Size3Price { get; set; }
}
...
[DataMember( Name = "Prices" )]
public PriceCollection[] Prices { get; set; }
...
}
On the Controller side I have the "Edit" ActionResult which receives the "EditedProductEntry" data structure:
[HttpPost]
[Route( "Edit", Name = "Product_Edit" )]
public ActionResult Edit( EditedProductEntry post )
{
...
}
I've been fiddling about with the "post" parameter's structure attempting to have it accept the "Prices" collection but this has me stumped. The rest of the data is coming through fine & is populating the "EditedProductEntry" object, however the collection is resolute in remaining null.
I've tried catching an object[], EditedProductEntry[], ICollection, IEnumerable & have also tried specifying these as an additional parameter to the "Edit" Action but so far I've not had any luck.
Does anyone have any other ideas?