mardi 24 mai 2016

How to enable jquery to insert multiple row to database which include @Html.DropDownListFor in the table

I'm new to mvc and jquery
When I use dropdownlist in this table, jquery code never work
this table without DropDownList, code of jquery work perfect
so I must be edit in jquery code to work perfect
this is my code

<div><a href="#" id="addNew">Add New</a></div>
<table id="dataTable" border="0" cellpadding="0" cellspacing="0">
    <tr>
        <th>color</th>
        <th>WindowType</th>
        <th>height</th>
        <th>width</th>
        <th></th>
    </tr>
    @if (Model != null && Model.Count > 0)
    {
        int j = 0;
        foreach (var item in Model)
        {
            <tr style="border:1px solid black">
                <td>@Html.DropDownListFor(m => m[j].Color, (SelectList)ViewBag.color)</td>
                <td>@Html.DropDownListFor(m => m[j].WindowType, (SelectList)ViewBag.WindowType)</td>
                <td>@Html.TextBoxFor(m => m[j].height, new { @class = "form-control" })</td>
                <td>@Html.TextBoxFor(m => m[j].width, new { @class = "form-control" })</td>
                <td>
                    @if (j > 0)
                    {
                        <a href="#" class="remove">Remove</a>
                    }
                </td>
            </tr>
                    j++;
        }
    }
</table>
    <input type="submit" value="Save Bulk Data" />

this in code of jquery

@section Scripts{
    @Scripts.Render("~/bundles/jqueryval")
    <script language="javascript">
        $(document).ready(function () {

            //1. Add new row
            $("#addNew").click(function (e) {
                e.preventDefault();
                var $tableBody = $("#dataTable");
                var $trLast = $tableBody.find("tr:last");
                var $trNew = $trLast.clone();

                var suffix = $trNew.find(':input:first').attr('name').match(/\d+/);
                $trNew.find("td:last").html('<a href="#" class="remove">Remove</a>');
                $.each($trNew.find(':input'), function (i, val) {
                    // Replaced Name
                    var oldN = $(this).attr('name');
                    var newN = oldN.replace('[' + suffix + ']', '[' + (parseInt(suffix) + 1) + ']');
                    $(this).attr('name', newN);
                    //Replaced value
                    var type = $(this).attr('type');
                    if (type.toLowerCase() == "text") {
                        $(this).attr('value', '');
                    }

                    // If you have another Type then replace with default value
                    $(this).removeClass("input-validation-error");

                });
                $trLast.after($trNew);

                // Re-assign Validation
                var form = $("form")
                    .removeData("validator")
                    .removeData("unobtrusiveValidation");
                $.validator.unobtrusive.parse(form);
            });

            // 2. Remove
            $('a.remove').live("click", function (e) {
                e.preventDefault();
                $(this).parent().parent().remove();
            });

        });
    </script>
}

Can some body help me?

Aucun commentaire:

Enregistrer un commentaire