【发布时间】:2017-01-06 18:27:14
【问题描述】:
我有以下表单,其中包含要发送到服务器的多维数组中的数据。问题是我无法在控制器中获取这些数据。
index.html
<form id="my-form" action="/Home/TestingMethod" method="post">
<table id="people" class="table table-striped">
<thead>
<tr>
<th>ID</th>
<th>Name</th>
<th>Owns Item</th>
</tr>
</thead>
<tbody>
<tr>
<td>1</td>
<td>Danny</td>
<td class="items">
<select name="PersonList[1]Item[]" class="form-control">
<option value=""></option>
<option value="Keys">Keys</option>
<option value="Phone">Phone</option>
</select>
</td>
</tr>
</tbody>
</table>
</form>
我的模特
public class MyModel
{
public List<int> PersonList { get; set; }
}
HomeController
[HttpPost]
public JsonResult TestingMethod(MyModel model)
{
List<int> list_of_people = model.PersonList;
return Json("I am the server, I got your data.");
}
问题在于 list_of_people 包含 0 个元素。
提交的表单数据
PersonList[1]Item[]:Phone
相关:how to access Javascript multidimensional array in MVC controller
【问题讨论】:
-
你的
PersonList是一个int的List,但是你select的值是一个字符串? -
我的表单中的
Person提供一个 int 1。Person[1] -
所有反对票是怎么回事?
-
那么你期待什么?
MyModel.PersonList中的整数列表? -
是的,我想根据我的 Person[1] 数据获取整数列表,暂时忽略 Item[] 值。
标签: javascript c# asp.net-mvc forms