MVC2 - 通过 JQuery 添加到表单的复选框在 JQuery 提交的表单集合中看不到

发布于 2024-12-20 18:58:54 字数 1824 浏览 1 评论 0原文

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

.spark 视图中的表如下所示。然后,使用 getJSON() 调用 JQuery 脚本,首先清除所有行,然后填充 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。因此,要么表单集合不包含数据,要么创建我不知道的字典时出现问题。

任何帮助将不胜感激。

谢谢!

I took over another project and have fixed everything else except this one problem. I think I've seen this before, but can't for the life of me remember what the solution was.

The table in the .spark view looks like this. Then a JQuery script, using getJSON() is called to first clear all of the rows and then populate the tbody, building out all of the rows and columns and adding a checkbox in the first column for each row - each checkbox is called '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> </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>

There's also a .click event handler to submit the form:

$('#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();
    }
});

The form is submitted to the controller action and I assumed that the checkboxes would have been passed back in the form collection.

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

        // vals is null here
    }

When vals is created, it's returned as null. So either the formcollection contains no data or there's an issue creating the dictionary that I'm not aware of.

Any help would be greatly appreciated.

Thanks!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

公布 2024-12-27 18:58:54

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

Checkboxes are only submitted as HTTP POST data when they are checked. Have you tried checking a couple of the boxes, and then seeing if they appear in the form collection?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文