1

Part of my view looks like

<table border="1" class="table table-hover">
                    <tr><th>Item</th><th>Description</th><th>Price</th><th>Quantity</th></tr>
                        <tr>
                            <td>Shirt</td>
                            <td>T-Shirt</td>
                            <td>5.0000</td>
                            <td><select class="form-control" id="items_1_" name="items[1]"><option value="">Select quantity</option>
<option value="0">0</option>
<option value="1">1</option>
<option value="2">2</option>
<option value="3">3</option>
<option value="4">4</option>
<option value="5">5</option>
</select></td>
                        </tr>
                        <tr>
                            <td>Shirt</td>
                            <td>Full-Sleeve</td>
                            <td>6.0000</td>
                            <td><select class="form-control" id="items_2_" name="items[2]"><option value="">Select quantity</option>
<option value="0">0</option>
<option value="1">1</option>
<option value="2">2</option>
<option value="3">3</option>
<option value="4">4</option>
<option value="5">5</option>
</select></td>
                        </tr>
                        <tr>
                            <td>Trousers</td>
                            <td>Half</td>
                            <td>4.0000</td>
                            <td><select class="form-control" id="items_3_" name="items[3]"><option value="">Select quantity</option>
<option value="0">0</option>
<option value="1">1</option>
<option value="2">2</option>
<option value="3">3</option>
<option value="4">4</option>
<option value="5">5</option>
</select></td>
                        </tr>
                        <tr>
                            <td>Trousers</td>
                            <td>Full</td>
                            <td>6.0000</td>
                            <td><select class="form-control" id="items_4_" name="items[4]"><option value="">Select quantity</option>
<option value="0">0</option>
<option value="1">1</option>
<option value="2">2</option>
<option value="3">3</option>
<option value="4">4</option>
<option value="5">5</option>
</select></td>
                        </tr>
                </table>

My controller looks like

[HttpPost]
public ActionResult RequestLaundry(int laundry, int pickuphour, int pickupminute, int[] items)
{
    var req = Request.Form;
    return View();
}

The data posted to controller from view looks like

{laundry=1&pickuphour=17&pickupminute=16&items%5b1%5d=5&items%5b2%5d=0&items%5b3%5d=1&items%5b4%5d=4}

Though all other model parameters are getting bound with default model binder, items is null. However, the post request looks fine to me. I am using ASP.NET MVC 5.0 with VS2015. Where could the problem be?

4
  • Share the Razor view of the posting form. Commented May 9, 2016 at 16:16
  • @Rusty The output of Request.Form is there towards the end of the post Commented May 9, 2016 at 16:18
  • @Holmes_Sherlock Is Items a list of string/ number or list of complex type? Share the model, it can help a lot. Commented May 9, 2016 at 16:21
  • I have posted the rendered view. items comes from select control. Commented May 9, 2016 at 16:23

1 Answer 1

2

You are missing items[0]
Array or list is zero based. Model binder is getting confused since it cant find the first item.

You can avoid using numbered indexer at all by using following syntax

name="items[]"
Sign up to request clarification or add additional context in comments.

4 Comments

Alternatively, just don't index any of these, i.e. name="items[]".
Thank you for the suggestion :)
Or (because its a simple property type) just name="items"
@StephenMuecke I didn't know that a simple property type can be consolidates as an array by default model binder. Tried and worked. Thanks.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.