首页 > 解决方案 > 执行另一个 ajax 请求后 Ajax 请求不起作用

问题描述

我有一个主视图,其中有 2 个表。使用 ajax,我调用了一个模态文本框窗口。我填写它们并单击添加。当模态窗口不是通过 ajax 而是简单地通过具有必要的 html 参数的按钮调用时,通常会添加记录,并且在 html.partial 的帮助下在主窗体上调用局部视图本身。我重新调用了 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();
        }

标签: jqueryajaxasp.net-mvc

解决方案


推荐阅读