I have a main view on which there are 2 tables. Using ajax, I invoke a modal textbox window. I fill them in and click add. Records were added normally when the modal window was called not by ajax but simply by a button with the necessary html parameters, and the partialview itself was called on the main form with the help of html.partial. I redid the call to ajax request, the ajax button request to add has stopped working. I can not understand what's the matter, please tell me what is wrong. Here is my code. main view:
@{
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>
}
view modal window:
<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">×</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>
Controller:
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();
}