首页 > 解决方案 > 如何将未知数量的表记录从视图传递回 ASP.NET 中的控制器

问题描述

我试图在提交表单后将一堆数据从我的视图传递给控制器​​。但是,我的表单有一个表,用户可以向其中添加记录,所以在他们完成表单之前,我不知道表中有多少记录。

我需要以某种方式将输入到表中的这些数据连同其他字段中的其他信息一起传递回控制器......这就是我到目前为止所做的:

@using (Html.BeginForm("SubmitForm", "Home", FormMethod.Post,
    new
    {
        traName = Request.Form["nameOfTra"],
        itemDesc = Request.Form[""],
        quantity = Request.Form[""],
        cost = Request.Form[""],
        amount = Request.Form["amountRequested"],
        memberName = Request.Form["commiteeMember"],
        date = Request.Form["agmDate"],
        signed = Request.Form["signed"],
        dated = Request.Form["dated"]
    }))
    {
    <h1 style="text-align: center;"> TRA grant application </h1>
    <h4 style="text-align: center;">This is the TRA Grant form for the association named below who agree to use these funds to cover the cost of administration of the TRA</h4>
    <p>
        <label for="nameOfTralbl">Name of TRA:</label>
        <input type="text" name="nameOfTra" value="" />
    </p>
    <h4> List of items the money will be spent on</h4>

    <table id="traTable">
        <tr>
            <td>Description of Items</td>
            <td>Quantity</td>
            <td>Cost</td>
        </tr>
        <tr>
            <td><input type='text' size="30" /></td>
            <td><input type='text' size="30" /></td>
            <td><input type='text' size="30" /></td>
        </tr>
    </table>
    <br />
    <button onclick="addRow()">Add Item</button>

    <script>
        function addRow() {
            var table = document.getElementById("traTable");
            var row = table.insertRow(-1);
            var cell1 = row.insertCell(0);
            var cell2 = row.insertCell(1);
            var cell3 = row.insertCell(2);

            cell1.innerHTML = "<input type='text' size='30'/>";
            cell2.innerHTML = "<input type='text' size='30'/>";
            cell3.innerHTML = "<input type='text' size='30'/>";
        }
    </script>

    if (@Model.uploaded != true)
    {
        <h2>Attach Documents:</h2>
        using (Html.BeginForm("Index",
                                       "Home",
                                       FormMethod.Post,
                                       new { enctype = "multipart/form-data", enctype2 = "multipart/form-data", enctype3 = "multipart/form-data" }))
        {
            <h4>Attach receipt:</h4>
            <input type="file" name="file" id="file" /><br>

            <br>
            <h4>Attach receipt log sheet:</h4>
            <input type="file" name="file2" id="file2" /><br>

            <br>
            <h4>Attach ETRA meeting minutes:</h4>
            <input type="file" name="file3" id="file3" /><br>

            <br>
            <input type="submit" value="Upload documents" />
        }
    }
    else
    {
        <h4>Documents have been successfully uploaded!</h4>

    }
    <p>
        <label for="amountRequestedlbl">Amount Requested (£):</label>
        <input type="text" name="amountRequested" value="" />
    </p>
    <p>
        <label for="commiteeMemberlbl">Name of committee member making application:</label>
        <input type="text" name="commiteeMember" value="" />
    </p>
    <br />




    <p>
        <label for="agmDatelbl">Date of AGM:</label>
        <input type="text" name="agmDate" value="" />
    </p>
    <p>
        <label for="signedlbl">Signed</label>
        <input type="text" name="signed" value="" />
    </p>
    <p>
        <label for="datedlbl">Dated</label>
        <input type="text" name="dated" value="" />
    </p>
    }


public ActionResult SubmitForm(string traName, string itemDesc, string quantity, string cost, float amount,
            string memberName, string date, string signed, string dated)
        {

            DBAccess dbAccess = new DBAccess();
            int reference = dbAccess.recordForm(traName, itemDesc, quantity, cost, amount, memberName, date, signed, dated);


            return View();

        }

但是,这当然不能作为itemDesc,quantitycost是表中的列,因此在填写表单时可能会有多条记录。

标签: asp.netasp.net-mvc-4

解决方案


您可以为客户端用户数据创建 JSON 并将 JSON 数据发布到控制器,然后使用 NewtonSoft.Json 库解析该数据。

用于将 JSON 转换为 DataTable 或 IEnumerable 的 Newtonsoft.Json 库


推荐阅读