0

I have a basic view that contains a dynamic table that allows the addition and deleting of rows. This consists of a field called Task and a field called sequence. I am using javascript to get these values and post to an array that I plan on having Ajax pass to the controller. When I run my code in Chrome the values are coming back blank. I've tried various methods I found on this site but haven't been able to find the answer. Can someone see what I am doing wrong or point me in the proper direction.

@using (Html.BeginForm())
{
    @Html.AntiForgeryToken()

    <div class="form-horizontal">
        <h4>IT ICNDB - Create New Forms</h4>
    <hr />
    @Html.ValidationSummary(true, "", new { @class = "text-danger" })


    <div class="row">
        @Html.LabelFor(model => model.FormName, htmlAttributes: new { @class = "col-md-2" })
        <div class="col-md-10">
            @Html.EditorFor(model => model.FormName, new { htmlAttributes = new { @class = "form-control", style = "width: 30%", id ="FormName" } })
            @Html.ValidationMessageFor(model => model.FormName, "", new { @class = "text-danger" })
        </div>
    </div>

    <br/>

    <div class="row">
        @Html.LabelFor(model => model.eSignReq, htmlAttributes: new { @class = "col-md-2" })
        <div class="col-md-10">
            <div class="checkbox">
                @Html.EditorFor(model => model.eSignReq, new {id = "eSignReq" })
                @Html.ValidationMessageFor(model => model.eSignReq, "", new { @class = "text-danger" })
            </div>
        </div>
    </div>

    <div class="row">
        @Html.LabelFor(model => model.DirectorEsign, htmlAttributes: new { @class = "col-md-2" })
        <div class="col-md-10">
            <div class="checkbox">
                @Html.EditorFor(model => model.DirectorEsign, new {id ="dirEsign"})
                @Html.ValidationMessageFor(model => model.DirectorEsign, "", new { @class = "text-danger" })
            </div>
        </div>
    </div>

    <br />

    <table id="Tasks">
        <thead>
            <tr>
                <th>
                    @Html.DisplayNameFor(model => model.Task)
                </th>
                <th>
                    @Html.DisplayNameFor(model => model.TaskSeq)
                </th>
                <th></th>
            </tr>
        </thead>
        <tbody>              
        <tr>
                <td>
                    @Html.TextBox("Task")
                </td>
                <td>
                    @Html.TextBox("Sequence")
                </td>
                <td>
                    <input type="button" class="BtnPlus" value="+"/>
                </td>
                <td>
                    <input type="button" , class="BtnMinus" value="-"/>
                </td>
            </tr>
        </tbody>
    </table>

    <br/>

    <div class="form-group">
        <div class="col-md-offset-2 col-md-10">
            <input type="submit" value="Create Form" class="button" id="SaveButton"/>
        </div>
    </div>
</div>
}


@section Scripts{
@Scripts.Render("~/bundles/jqueryval")
@Scripts.Render("~/Scripts/jquery-ui-1.12.1.js")

<script type="text/javascript">
    $(document).ready(function() {
        function addRow() {
            var html = '<tr>' +
                '<td> @Html.TextBox("Task",null) </td>' +
                '<td> @Html.TextBox("Sequence", null)</td>' +
                '<td> <input type="button" class="BtnPlus" value="+" /> </td>' +
                '<td> <input type="button" , class="BtnMinus" value="-" /> </td>' +
                '</tr>';
            $(html).appendTo($("tbody"));
        };


        function deleteRow() {
            var par = $(this).closest('tr');
            par.remove();
        };

        $("#SaveButton").click(function () {

            var data = Array();

            for (var i = 0; i < $('#Tasks').find("tr").length; i++) {
                data[i] = Array();
               var currentRow = $('#Tasks').find("tr").eq(i);

                for (j = 0; j < currentRow.find('td').length; j++) {
                   data[i][j] = currentRow.find('td').eq(j).val();
                };
            };
            alert(data);

            });

        $("tbody").on("click", ".BtnMinus", deleteRow);
        $("tbody").on("click", ".BtnPlus", addRow);
    });
</script>
}
Rygar
  • 1
  • There are multiple issues with your code. Refer to [Submit same Partial View called multiple times data to controller?](https://stackoverflow.com/questions/28019793/submit-same-partial-view-called-multiple-times-data-to-controller/28081308#28081308) for some options, and [A Partial View passing a collection using the Html.BeginCollectionItem helper](https://stackoverflow.com/questions/40539321/a-partial-view-passing-a-collection-using-the-html-begincollectionitem-helper/40541892#40541892) for a detailed implementation using `BeginCollectionItem` –  Oct 15 '18 at 22:41
  • I appreciate the links and will review them. Could I have you point out the flaws so that I can learn from those issues? I have been trying to teach myself and using stack overflow to find where I screw up. – Rygar Oct 15 '18 at 23:43

0 Answers0