core 다 듣고 mvc db first로 게시판 만들어가는중에 질문이 있어서 남깁니다.
1. 상세 페이지에서 삭제버튼을 만들어서 클릭이후 삭제페이지를 넘기지 않고 삭제하는 방법을 찾았으나 잘 안되는 점입니다. (Ajax로 넘기면 404 error, 500 error 납니다.)
2. controller 삭제부분의 문제점이 있는것 같은데 어떻게 수정해야할지 궁금합니다.
아래에 Controller 소스와 Details소스입니다.
cotroller 소스
#region 삭제
// GET: DotNetNote/Delete/5
[ValidateAntiForgeryToken]
public JsonResult Delete(int? id)
{
DotNetNoteArticle dotNetNoteArticle = db.DotNetNoteArticles.Find(id);
if (dotNetNoteArticle == null)
{
return Json(new { success = false }, JsonRequestBehavior.AllowGet);
}
db.DotNetNoteArticles.Remove(dotNetNoteArticle);
db.SaveChanges();
return Json(new { success = true }, JsonRequestBehavior.AllowGet);
}
// POST: DotNetNote/Delete/5
[HttpPost, ActionName("Delete")]
[ValidateAntiForgeryToken]
public JsonResult DeleteConfirmed(int id)
{
DotNetNoteArticle dotNetNoteArticle = db.DotNetNoteArticles.Find(id);
if (dotNetNoteArticle == null)
{
return Json(new { success = false }, JsonRequestBehavior.AllowGet);
}
db.DotNetNoteArticles.Remove(dotNetNoteArticle);
db.SaveChanges();
return Json(new { success = true }, JsonRequestBehavior.AllowGet);
}
#endregion
Details 소스
@model DotNetNoteJqGrid.Models.DotNetNoteArticle
@{
ViewBag.Title = "Details";
}
<h2>상세보기</h2>
<script src="~/Scripts/jquery-3.4.1.min.js"></script>
<script src="~/Scripts/ckeditor/ckeditor.js"></script>
<form action="/DotNetNote/Edit/@Model.Num" method="post" enctype="multipart/form-data">
<div class="form-horizontal">
@Html.AntiForgeryToken()
<hr />
<div class="form-group">
<label class="control-label col-md-2" for="Name">Name</label>
<div class="col-md-10">
<p class="form-control text-box single-line" /> @Model.Name
</div>
</div>
<div class="form-group">
<label class="control-label col-md-2" for="Title">Title</label>
<div class="col-md-10">
<p class="form-control text-box single-line" />@Model.Title
</div>
</div>
<div class="form-group">
<label class="control-label col-md-2" for="Content">Content</label>
<div class="col-md-10">
<textarea class="form-control ckeditor" id="Content" name="Content">@Model.Content</textarea>
</div>
</div>
<div class="form-group">
<label class="control-label col-md-2" for="Tags">Tags</label>
<div class="col-md-10">
<p class="form-control text-box single-line" />@Model.Tags
</div>
</div>
<div class="form-group">
<label class="control-label col-md-2" for="Password">Password</label>
<div class="col-md-10">
<p class="form-control text-box single-line" />@Model.Password
</div>
</div>
<div class="form-group">
<label class="control-label col-md-2" for="Email">Email</label>
<div class="col-md-10">
<p class="form-control text-box single-line" />@Model.Email
</div>
</div>
<div class="form-group">
<label class="control-label col-md-2" for="Homepage">Homepage</label>
<div class="col-md-10">
<p class="form-control text-box single-line" />@Model.Homepage
</div>
</div>
<div class="form-group">
<label class="control-label col-md-2" for="ReadCount">ReadCount</label>
<div class="col-md-10">
<p class="form-control text-box single-line" data-val="true" data-val-number="ReadCount 필드는 숫자여야 합니다." data-val-required="ReadCount 필드가 필요합니다." />@Model.ReadCount
</div>
</div>
<div class="form-group">
<label class="control-label col-md-2" for="FileUpload">Upload File</label>
<div class="col-md-10">
<input type="file" id="file" name="file" class="form-control-file" />
<span class="field-validation-valid text-danger" data-valmsg-for="file" data-valmsg-replace="true"></span>
</div>
</div>
</div>
</form>
<p>
<div class="col-lg-6 col-sm-12 text-right">
<a class="btn btn-success btn-sm ml-2" href="/DotNetNote/Edit/@Model.Num">수정</a>
<button id="btnDelete" class="btn btn-secondary btn-sm ml-2">삭제</button>
</div>
<div class="col-lg-6 col-sm-12">
<button class="btn btn-primary btn-sm" onclick="location.href='/DotNetNote/Index'">목록</button>
</div>
</p>
<script>
$(document).ready(function () {
$('#btnDelete').click(function () {
var id = @Model.Num; // 삭제할 아이템의 ID 값을 가져옵니다.
// AJAX 요청 설정
$.ajax({
url: '/DotNetNote/Delete/' + id,
type: 'POST',
dataType: 'json',
success: function (result) {
if (result.success) {
// 삭제 성공 시, 필요한 처리를 수행합니다.
alert('삭제되었습니다.');
window.location.href = '/DotNetNote/Index'; // 목록 페이지로 이동
} else {
// 삭제 실패 시, 필요한 처리를 수행합니다.
alert('삭제에 실패했습니다.');
}
},
error: function () {
alert('오류가 발생했습니다.');
}
});
});
});
</script>