如果我单击“编辑”按钮,将弹出一个模态弹出窗口,当我单击“保存更改”按钮时,只要更改文本框的值(标题),我就可以获取ID,标题和存档BUT,不会获取ID(用户)和存档(0或1),但我可以获取标题。怎么可能呢?
我将数据从数据库传递到了“编辑”按钮:
<button type="button" class="btn btn-primary float-right edit-modal"
data-archive="@y" data-status="@item.status" data-
title="@item.discussion_title"
data-id="@item.offered_course_discussion_id" data-toggle="modal" data-
target="#NotArchiveModal">
Edit
</button>
这是我的模态:
<div class="modal fade edit-modal" id="NotArchiveModal" tabindex="-1"
role="dialog" aria-labelledby="NotArchiveModal" aria-hidden="true">
<div class="modal-dialog" role="document">
<div class="modal-content">
<div class="modal-header">
<h5 class="modal-title" id="exampleModalLabel">Edit
Topic
</h5>
<button type="button" class="close" data-
dismiss="modal" aria-label="Close">
<span aria-hidden="true">×</span>
</button>
</div>
<div class="modal-body">
<input type="hidden"
id="offered_course_discussion_id"
name="offered_course_discussion_id" value="" />
<input type="text" id="item_discussion_title"
name="item_discussion_title" class="form-control"
value="" />
<div class="form-check">
<input class="form-check-input" type="checkbox"
id="offered_course_discussion_archive"
name="archive" value="">
<label class="form-check-label"
for="defaultCheck1">
@thread Thread
</label>
<br />
<button type="submit" id="submitChanges"
onclick="myfunction()" value="click" class="btn
btn-primary">
Save changes
</button>
</div>
</div>
</div>
</div>
</div>
这是我的Jquery:
<script>
$(document).on("click", ".edit-modal", function () {
var title = $(this).data('title');
var id = $(this).data('id');
var archive = $(this).data('archive');
$(".modal-body #item_discussion_title").val(title);
$(".modal-body #offered_course_discussion_id").val(id);
$(".modal-body #offered_course_discussion_archive").val(archive);
});
function myfunction() {
var id = $(".modal-body #offered_course_discussion_id").val();
var title = $(".modal-body #item_discussion_title").val();
var archive = $(".modal-body
#offered_course_discussion_archive").val();
alert("ID: " + id + " Title: " + title + " Archive: " + archive);
$.ajax({
url: 'UpdateTopic',
type: 'POST',
contentType: 'application/json; charset=utf-8',
data: {
"id": id,
"title": title,
"archive": archive
},
success: function (data) {
alert(data);
}
})
}
</script>
我的控制器:
public JsonResult UpdateTopic(int id, string title, int archive)
{
}