执行另一个ajax请求后Ajax请求不起作用

时间:2018-12-21 05:33:26

标签: jquery ajax asp.net-mvc

我有一个主视图,上面有2个表。使用ajax,我调用一个模式文本框窗口。我填写它们,然后单击添加。正常情况下,不是通过ajax调用模态窗口时,而是通过带有必要html参数的按钮调用记录时,通常会添加记录,并在html.partial的帮助下在主窗体上调用partialview本身。我将调用重命名为ajax请求,添加的ajax按钮请求已停止工作。我不明白怎么了,请告诉我哪里出了问题。这是我的代码。主视图:

@{
    ViewBag.Title = "Index";
}

@model IEnumerable<AjaxTest.Models.Book>

<h2>Каталог книг</h2>

<input id="modRender" type="submit" value="Добавить" />

<body>
    <div id="result"></div>
    <div>
        <table id="tab" class="table table-bordered">
            <thead>
                <tr>
                    <th>Название</th>
                    <th>Автор</th>
                    <th>Цена</th>
                </tr>
            </thead>
            <tbody>
                @foreach (var item in Model)
                {
                    <tr>
                        <td>
                            @Html.DisplayFor(i => item.Name)
                        </td>
                        <td>
                            @Html.DisplayFor(i => item.Author)
                        </td>
                        <td>
                            @Html.DisplayFor(i => item.Price)
                        </td>
                    </tr>
                }
            </tbody>
        </table>
    </div>
</body>

<h2>Список пользователей</h2>

<body>
    <div>
        <table class="table table-bordered">
            <thead>
                <tr>
                    <th>Имя</th>
                    <th>Фамилия</th>
                    <th>Возраст</th>
                </tr>
            </thead>
            <tbody>
                @foreach (var item in ViewBag.Users as List<AjaxTest.Models.User>)
                {
                    <tr>
                        <td>
                            @Html.DisplayFor(i => item.Name)
                        </td>
                        <td>
                            @Html.DisplayFor(i => item.Surname)
                        </td>
                        <td>
                            @Html.DisplayFor(i => item.Age)
                        </td>
                    </tr>
                }
            </tbody>
        </table>
    </div>
</body>

<div id="result"></div>

@section scripts{
    <script type="text/javascript">
        $('#modRender').on('click', function () {
            $.ajax({
                type: "POST",
                url: "/Home/ModalCreate",
                success: function (data) {
                    $('#result').html(data);
                    $('#mod').modal('show');
                }
            });
        });

        $('#btnAdd').on('click', function () {
            $.ajax({
                type: "POST",
                url: "/Home/Create",
                data: {
                    "Name": $('#txtName').val(),
                    "Author": $('#txtAuthor').val(),
                    "Price": $('#txtPrice').val()
                },
                success: function (data) {
                    $('#tab tbody').append(data);
                }
            });
        });

        $("#result").on('hidden.bs.modal', function () {
            $('#mod').remove();
        });
    </script>
}

查看模式窗口:

<div id="mod" class="modal fade" >
    <div class="modal-dialog" role="document">
        <div class="modal-content">
            <div class="modal-header">
                <button type="button" class="close" data-dismiss="modal" aria-label="Close">
                    <span aria-hidden="true">&times;</span>
                </button>
                <h4 class="modal-title">Новая книга</h4>
            </div>
            <div class="modal-body">
                <table>
                    <tr>
                        <td><p>Название</p></td>
                        <td><input type="text" id="txtName" /></td>
                    </tr>
                    <tr>
                        <td><p>Автор</p></td>
                        <td><input type="text" id="txtAuthor" /></td>
                    </tr>
                    <tr>
                        <td><p>Цена</p></td>
                        <td><input type="text" id="txtPrice" /></td>
                    </tr>
                </table>
            </div>
            <div class="modal-footer">
                <button type="button" class="btn btn-secondary" data-dismiss="modal">Закрыть</button>
                <button id="btnAdd" type="submit" data-dismiss="modal" class="btn btn-primary">Добавить</button>
            </div>
        </div>
    </div>
</div>

控制器:

Context db = new Context();

        [HttpGet]
        public ActionResult Index()
        {
            List<Book> books = db.Books.ToList();
            List<User> users = db.Users.ToList();
            ViewBag.Users = users;

            return View(books);
        }

        [HttpPost]
        public ActionResult Create(Book book)
        {
            db.Books.Add(book);
            db.SaveChanges();

            return PartialView(book);
        }

        [HttpPost]
        public ActionResult ModalCreate()
        {
            return PartialView();
        }

0 个答案:

没有答案