简体   繁体   中英

How to catch posted complex structure in MVC

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?

I tried the same case on my local machine and figured out that the indexing of the control names is not correct.

The control names should be following zero based index and so the data which is being posted too.

The input controls should be declared as following.

<input name="Prices[0].Price" id="Prices_1__Price" value="9.99" type="text">
<input name="Prices[0].Size2Price" id="Prices_1__Size2Price" value="0.00" type="text">
<input name="Prices[0].Size3Price" id="Prices_1__Size3Price" value="0.00" type="text">
<input name="Prices[1].Price" id="Prices_2__Price" value="5.00" type="text">
<input name="Prices[1].Size2Price" id="Prices_2__Size2Price" value="0.00" type="text">
<input name="Prices[1].Size3Price" id="Prices_2__Size3Price" value="0.00" type="text">
<input name="Prices[2].Price" id="Prices_3__Price" value="0.00" type="text">
<input name="Prices[2].Size2Price" id="Prices_3__Size2Price" value="0.00" type="text">
<input name="Prices[2].Size3Price" id="Prices_3__Size3Price" value="0.00" type="text">

So the log file also will have following data.

"Prices[0].Price": "9.9900",
"Prices[0].Size2Price": "0.0000",
"Prices[0].Size3Price": "0.0000",
"Prices[1].Price": "5.5800",
"Prices[1].Size2Price": "0.0000",
"Prices[1].Size3Price": "0.0000",
"Prices[2].Price": "0.0000",
"Prices[2].Size2Price": "0.0000",
"Prices[2].Size3Price": "0.0000",

Making this change should resolve your issue.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM