我的部分观点看起来像
<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>
我的控制器看起来像
[HttpPost]
public ActionResult RequestLaundry(int laundry, int pickuphour, int pickupminute, int[] items)
{
var req = Request.Form;
return View();
}
从视图发布到控制器的数据类似于
{洗衣= 1&安培; pickuphour = 17&安培; pickupminute = 16&安培;项%5B1%5D = 5&安培;项%5B2%5D = 0&安培;项%5B3%5D = 1&安培;项%5B4%5D = 4}
虽然所有其他模型参数都与默认模型绑定器绑定,但items
为null
。但是,帖子请求对我来说很好。我在VS2015上使用ASP.NET MVC 5.0。问题出在哪里?
答案 0 :(得分:1)
你缺少物品[0]
数组或列表基于零。模型绑定器因为无法找到第一个项目而变得混乱。
您可以使用以下语法
name="items[]"