【问题标题】:Unable to read submitted form data from MVC controller无法从 MVC 控制器读取提交的表单数据
【发布时间】: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


【解决方案1】:

您的选择字段的命名约定不正确。为了匹配您的模型结构,它应该如下所示:

<select name="PersonList[0]" class="form-control">
<select name="PersonList[1]" class="form-control">
<select name="PersonList[2]" class="form-control">
...

因为PersonList 属性只是一个整数数组。如果您希望能够绑定到整数,还要确保发送整数值:

<option value="0">Keys</option>
<option value="1">Phone</option>
...

如果您想允许空值,请确保您的列表被定义为可为空的整数:

public class MyModel
{
    public List<int?> PersonList { get; set; }
}

现在你可以这样做了:

<option value=""></option>
<option value="0">Keys</option>
<option value="1">Phone</option>
...

另一方面,如果它是一个复杂的属性:

public class MyModel
{
    public List<Person> PersonList { get; set; }
}

Person 的定义如下:

public class Person
{
    public List<Item> Items { get; set; }
}

那么你可以这样做:

<select name="PersonList[0].Items[0].SomeProperty" class="form-control">
<select name="PersonList[0].Items[1].SomeProperty" class="form-control">
<select name="PersonList[1].Items[0].SomeProperty" class="form-control">
...

我还建议您阅读 following post,它解释了模型绑定器的工作原理以及它所期望的命名约定。

【讨论】:

  • 是的,这正是我所追求的,它是我拥有的自定义表单,它不能直接/轻松地使用标准活页夹映射到我的模型。我会试一试,看看我能不能让它工作。
  • 我已经对 PersonList 进行了修改,但我的控制器仍然没有收到它。 model.PersonList.Count = 0
  • 由于 PersonList 是一个整数数组,请确保您的选项也是整数:&lt;option value="0"&gt;Keys&lt;/option&gt;&lt;option value="1"&gt;Phone&lt;/option&gt;。另一个重要方面是索引应该从 0 开始,而不是 1,如我的代码所示:&lt;select name="PersonList[0]" class="form-control"&gt;
  • 好吧,我的整数不是从 0 开始的,因为这与我数据库中的 ID 有关。它必须从 0 开始才能工作吗?
  • 我说的是索引,而不是值。您很可能拥有以下内容:&lt;select name="PersonList[0]" class="form-control"&gt;&lt;option value="12345"&gt;Keys&lt;/option&gt;&lt;/select&gt;。现在 PersonList 集合的第一个元素(在索引 0 处)将具有 12345 的值,它可以反映数据库中的 ID。但是索引应该总是从 0 开始,这就是 .NET 中列表和数组的工作方式。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-04-02
  • 1970-01-01
  • 2017-03-29
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多