【问题标题】:MVC2 - Checkboxes added to form via JQuery not seen in formcollection on JQuery submitMVC2 - 通过 JQuery 添加到表单的复选框在 JQuery 提交的 formcollection 中看不到
【发布时间】:2011-12-11 15:02:59
【问题描述】:

我接手了另一个项目并修复了除这个问题之外的所有其他问题。我想我以前见过这个,但我一生都记不起解决方案是什么。

.spark 视图中的表格如下所示。然后调用一个 JQuery 脚本,使用 getJSON() 首先清除所有行,然后填充 tbody,构建所有行和列,并在每一行的第一列中添加一个复选框 - 每个复选框都称为“testitem” '。

<form id='new_test_items' name='new_test_items' method='post' action='${Url.Action("AddTestItems", new { id = Model.TestID })}'>
    <table id='component_list' name='component_list' class='component_list_table striped_table' cellpadding='0' border='0' cellspacing='0'>
        <thead>
            <tr>
                <th>&nbsp;</th>
                <th>Column1</th>
                <th>Column2</th>
                <th>Column3</th>
                <th>Column4</th>
                <th>Column5</th>
                <th>Column6</th>
                <th>Column7</th>
                <th>Column8</th>
            </tr>
        </thead>
        <tbody>
        </tbody>
    </table>
</form>

还有一个 .click 事件处理程序来提交表单:

$('#add_selected').click(function() {

    var $items = $('#component_list input:checkbox:checked');

    // If none checked, alert
    if ($items.length == 0) {
        alert('No items have been selected.');
        return false;
    }
    else {
        $('#new_test_items').submit();
    }
});

表单已提交给控制器操作,我假设复选框会在表单集合中传回。

    public ActionResult AddTestItems(int id, FormCollection coll)
    {
        Dictionary<string, ValueProviderResult> vals = coll.ToValueProvider() as Dictionary<string, ValueProviderResult>;

        // vals is null here
    }

创建 vals 时,它返回为 null。因此,要么 formcollection 不包含数据,要么创建我不知道的字典存在问题。

任何帮助将不胜感激。

谢谢!

【问题讨论】:

  • 输入是否指定了名称属性?
  • @PhilKlein 对不起 --- 是的,'testitem'。
  • 使用 FireBug(或类似的)你能验证浏览器实际上是在发送 POST 数据中的输入吗?

标签: jquery asp.net-mvc spark-view-engine value-provider


【解决方案1】:

复选框仅在选中时作为 HTTP POST 数据提交。您是否尝试过检查几个框,然后查看它们是否出现在表单集合中?

【讨论】:

    猜你喜欢
    • 2013-02-12
    • 1970-01-01
    • 1970-01-01
    • 2018-09-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多