将jQuery数据表值绑定到控制器参数

时间:2017-07-19 19:03:41

标签: c# asp.net-mvc datatables

我有一个控制器,它返回一个具有List<string>属性的视图模型,并在jQuery数据表中以一系列文本框显示结果。我想允许用户编辑列表,并将更新的数据发布回控制器。如果我停留在数据表的第一页上,这是正常的,但是如果我导航到下一页然后编辑其中一个字段,则视图模型上的List<string>属性是控制器中的空列表动作。

这是我的观点模型

public class ViewModel
{
    public List<string> Values { get; set; }

    public ViewModel()
    {
        this.Values = new List<string>();
    }
}

我的控制器操作

public ActionResult Edit()
{
    ViewModel viewModel = new ViewModel();
    viewModel.Values.Add("ST0001");
    viewModel.Values.Add("ST0002");
    viewModel.Values.Add("ST0003");
    viewModel.Values.Add("ST0004");
    viewModel.Values.Add("ST0005");
    viewModel.Values.Add("ST0006");
    viewModel.Values.Add("ST0007");
    viewModel.Values.Add("ST0008");
    viewModel.Values.Add("ST0009");
    viewModel.Values.Add("ST0010");

    return View(viewModel);
}

[HttpPost]
public ActionResult Edit(ViewModel viewModel)
{
    // I have a breakpoint here to examine the values of viewModel.Values
    // As long as I stay on the first page of the data table, viewModel.Values contains the updated values I type
    // If I go to the second page of the table, viewModels.Values is an empty list
    return RedirectToAction("Edit", viewModel);
}

这里是视图中的数据表

<table id="myTable" class="table">
    <thead>
        <tr>
            <th>
                @Html.Label("Value")
            </th>
        </tr>
    </thead>
    <tbody>
        @if (Model != null)
        {
            for (int i = 0; i < Model.Values.Count; i++)
            {
                <tr>
                    <td>
                        @Html.TextBoxFor(model => Model.Values[i])
                    </td>
                </tr>
            }
        }
    </tbody>
</table>

@section Scripts
{
    <script type="text/javascript">
        $(function () {
            $('#myTable').dataTable({
                'order': [[0, 'asc']],
                'pageLength': 5
            });
        });
    </script>
}

<input>&#39; s id在数据表的每个页面上看起来都是正确的。

Here is what an input control on the first page looks like

Here is what an input control on the second page looks like

我假设问题是如果提交的数据从非零索引开始,ASP不会将List参数加载到控制器?有没有办法解决这个问题,比如从我的控制器查看所有提交的表单数据并手动将每个数组元素加载到列表中的适当位置?

0 个答案:

没有答案