代码之家  ›  专栏  ›  技术社区  ›  Dimitri

将HTML表内容获取到MVC控制器

  •  0
  • Dimitri  · 技术社区  · 7 年前

    更新此日志的底部

    我有一个HTML表,它看起来像:

    <table class="table table-hover" id="selectedProductsForContract">
        <thead>
            <tr>
                <th>Product</th>
                <th>Quantity</th>
            </tr>
        </thead>
        <tbody>
            <tr>
                <td class="col-md-8">
                    <p>Product name</p>
                </td>
                <td class="col-md-1">
                    <input type="number" class="form-control" value="1">
                </td>
                <td class="col-md-2">
                    <button type="button" class="btn btn-danger">
                        Remove
                    </button>
                </td>
            </tr>
    
        </tbody>
    </table>
    

    我打算用所选列表框中的值动态填充。我正在使用生成列表框 @Html.ListBox()

    @Html.ListBox("allProducts", allProductsForSupplier, new { ID = "allProductsListbox", @class = "form-control", @onclick = "AddSelectedProductToTable()" })
    

    这个 onclick 列表框的事件如下:

    <script>
        function AddSelectedProductToTable() {
    
        var selectedProduct = $("#allProductsListbox option:selected").text();    
    
        $("#selectedProductsForContract").find('tbody')
            .append($('<tr>' +
                '<td class="col-md-8"><p>' + selectedProduct + '</p></td>' +
                '<td class="col-md-1"> <input type="number" class="form-control" value="1"> </td>' +
                '<td class="col-md-2">  <button type="button" class="btn btn-danger" onclick="RemoveSelectedProductFromTable(this)"> Remove </button > </td>' +
                '</tr>'));
    
        }
    </script>
    

    这完全符合我的要求,并产生以下结果: enter image description here

    一旦填充了表,我需要将所选的产品名称和数量发布回服务器。

    我尝试过几种方法,但最接近的方法是使用以下方法:

    <script type="text/javascript">
        $(function () {
          $("#btnInsert").click(function () {
            var itemlist = [];
    
            //get cell values, instead of the header text.
            $("table tr:not(:first)").each(function () {
                var tdlist = $(this).find("td");
                var Item = { ID: $(tdlist[0]).html(), Name: $(tdlist[1]).html() };
                itemlist.push(Item);
            })
    
            $.ajax({
                url: '@Url.Action("InsertValue", "Contract")', //
                dataType: "json",
                data: JSON.stringify({ itemlist: itemlist }),
                type: "POST",
                contentType: "application/json; charset=utf-8",
                success: function (result) {
                    alert("success");
                },
                error: function (xhr) {
                    alert("error");
                }
            });
          });
        });
    </script>
    

    哪个岗位回到下面的控制器:

    [HttpPost]
    public JsonResult InsertValue(Item[] itemlist)
    {
        foreach (var i in itemlist)
        {
            //loop through the array and insert value into database.
        }
        return Json("Ok");
    }
    

    并产生以下输出: enter image description here

    我认为有一种比我现在做的更好的方法,但是在这里我有点迷失了,我认为用regex或字符串操作解析HTML并不是这里的答案…

    有人能建议/指导我做我想做的事吗? 如果有任何问题,请提问。

    亲切的问候!

    更新 由于@adyson,我已将addSelectedProductToTable()更改为以下内容:

     $("table tr:not(:first)").each(function () {
    
            var tdlist = $(this).find("td");
            var input = $(tdlist[1]).find("input");
    
            var Item = { ID: $(tdlist[0]).text(), Name: $(input[0]).val() };
            itemlist.push(Item);   
    
        })
    

    在哪里? var input = $(tdlist[1]).find("input"); 已添加,我需要更改 文本() Val.() 本部分: Name: $(input[0]).val() 以便能够检索值。

    非常感谢Adyson!

    1 回复  |  直到 7 年前
        1
  •  1
  •   ADyson    7 年前

    .text() td .html() .val()

    var tb = $(tdlist[1]).find("input") tb.val()