繁体   English   中英

C#MVC5 BulkData插入不适用于部分视图

[英]C# MVC5 BulkData Insert doesn't work on partial view

我在MVC5中从局部视图插入批量数据时遇到问题。 当它不是局部视图时,它可以完全正常工作。 我正在使用此示例: http : //www.dotnetawesome.com/2014/08/how-to-insert-multiple-record-to-database-at-a-time-aspnet-mvc4.html这是原始代码从控制器:

public ActionResult BulkData()
    {
            // This is only for show by default one row for insert data to the database
            List<ContactInfo> ci = new List<ContactInfo> {new ContactInfo{ ID = 0, ContactName = "", ContactNo=""} };
            return View(ci);
    }

我将其更改为(因为我想在bootrap模态弹出窗口中使用该功能):

public ActionResult BulkData()
    {
            // This is only for show by default one row for insert data to the database
            List<ContactInfo> ci = new List<ContactInfo> {new ContactInfo{ ID = 0, ContactName = "", ContactNo=""} };
            return PartialView(ci);
    }

更改后不起作用。 我不明白为什么。 我还添加了所有脚本,从布局到局部视图,但这也无济于事。 这是视图中的代码:

@model List<MVCBulkInsert.ContactInfo>
@{
    ViewBag.Title = "Insert Bulk Data";
}
<style>
    th {
        text-align:left;
    }
    td {
    padding:5px;
}
</style>
<div style="width:700px; padding:5px; background-color:white;">
    @using (Html.BeginForm("BulkData","Save", FormMethod.Post))
    {
        @Html.AntiForgeryToken()
        @Html.ValidationSummary(true)

        if (ViewBag.Message != null)
        {
            <div style="border:solid 1px green">
                @ViewBag.Message
            </div>
        }

        <div><a href="#" id="addNew">Add New</a></div>
        <table id="dataTable" border="0" cellpadding="0" cellspacing="0">
            <tr>
               <th>Contact Name</th>
                <th>Contact No</th>
                <th></th>
            </tr>
            @if (Model != null && Model.Count > 0)
            {
                int j = 0;
                foreach (var i in Model)
                {
                    <tr style="border:1px solid black">
                        <td>@Html.TextBoxFor(a=>a[j].ContactName)</td>
                        <td>@Html.TextBoxFor(a=>a[j].ContactNo)</td>
                        <td>
                            @if (j > 0)
                            {
                                <a href="#" class="remove">Remove</a>
                            }
                        </td>
                    </tr>
                    j++;
                }
            }
        </table>
        <input type="submit" value="Save Bulk Data" />
    }
</div>

@* Here I will add Jquery Code for validation / dynamically add new rows / Remove rows etc *@

@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>
}

问题出在哪里?

问题在于部分视图无法识别脚本部分。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM