制作删除请求AJAX(ASP.NET MVC)(Make delete request AJAX(ASP.NET MVC))

我有div的视图,我在那里显示来自AJAX调用的数据

这是后端代码

[HttpGet] public ActionResult EmailsList() { var itemsEmail = db.InvitationMails .Select(x=> new { Id = x.Individ_Id, Email = x.To.ToString(), Name = x.Name.ToString(), }) .ToList(); return Json(itemsEmail, JsonRequestBehavior.AllowGet); }

这是View上的代码(AJAX)

<script> $(document).ready(function () { email_update(); }); function email_update() { $.ajax({ url: '@Url.Action("EmailsList", "Questions")', contentType: 'application/json; charset=utf-8', type: 'GET', dataType: 'json', processData: false, success: function (result) { var email = result; // console.log(result[0].Name); for (var i = 0; i <= email.length - 1; i++) { var editImage = '@Url.Content("~/Images/Edit.png")'; var deleteImage = '@Url.Content("~/Images/Delete.png")'; var emailHTML = '<div style="margin-left: 25px; margin-top: 10px;>' + '<b style="margin-left: 10px;">' +(i + 1) + '<b style="margin-left:20px;">' + result[i].Email + '</b>'+ '<b>' + '<b style="margin-left: 20px;">' + result[i].Name + '</b>' + '<a style="float: right; margin-right: 20px;">' + '<img src="' + editImage+ '">' + '</a>' + '<a style="float: right; margin-right: 20px;">' + '<img src="' + deleteImage + '">' + '</a>' + '</div>'; $(".email_list").append(emailHTML); } } }); }

这里是View代码(我从AJAX调用中附加代码)

<div class="email_list" style="height: 60%; width: 100%; overflow: auto;margin-bottom: 25px;"> </div>

我有删除按钮,我需要将元素的id传递给控制器​​。

这是控制器中的代码

public ActionResult Delete(int? id) { if (id == null) { return new HttpStatusCodeResult(HttpStatusCode.BadRequest); } Question questions = db.Questions.Find(id); if (questions == null) { return HttpNotFound(); } return View(questions); } // POST: Questions/Delete/5 [HttpPost, ActionName("Delete")] [ValidateAntiForgeryToken] public ActionResult DeleteConfirmed(int id) { Question questions = db.Questions.Find(id); db.Questions.Remove(questions); db.SaveChanges(); return RedirectToAction("Index"); }

I have view with div , where I display data from AJAX call

Here is back-end code

[HttpGet] public ActionResult EmailsList() { var itemsEmail = db.InvitationMails .Select(x=> new { Id = x.Individ_Id, Email = x.To.ToString(), Name = x.Name.ToString(), }) .ToList(); return Json(itemsEmail, JsonRequestBehavior.AllowGet); }

Here is code on View(AJAX)

<script> $(document).ready(function () { email_update(); }); function email_update() { $.ajax({ url: '@Url.Action("EmailsList", "Questions")', contentType: 'application/json; charset=utf-8', type: 'GET', dataType: 'json', processData: false, success: function (result) { var email = result; // console.log(result[0].Name); for (var i = 0; i <= email.length - 1; i++) { var editImage = '@Url.Content("~/Images/Edit.png")'; var deleteImage = '@Url.Content("~/Images/Delete.png")'; var emailHTML = '<div style="margin-left: 25px; margin-top: 10px;>' + '<b style="margin-left: 10px;">' +(i + 1) + '<b style="margin-left:20px;">' + result[i].Email + '</b>'+ '<b>' + '<b style="margin-left: 20px;">' + result[i].Name + '</b>' + '<a style="float: right; margin-right: 20px;">' + '<img src="' + editImage+ '">' + '</a>' + '<a style="float: right; margin-right: 20px;">' + '<img src="' + deleteImage + '">' + '</a>' + '</div>'; $(".email_list").append(emailHTML); } } }); }

And here is View code (where i append code from AJAX call)

<div class="email_list" style="height: 60%; width: 100%; overflow: auto;margin-bottom: 25px;"> </div>

I have delete button on what I need to pass id of element to controller.

Here is code in controller

public ActionResult Delete(int? id) { if (id == null) { return new HttpStatusCodeResult(HttpStatusCode.BadRequest); } Question questions = db.Questions.Find(id); if (questions == null) { return HttpNotFound(); } return View(questions); } // POST: Questions/Delete/5 [HttpPost, ActionName("Delete")] [ValidateAntiForgeryToken] public ActionResult DeleteConfirmed(int id) { Question questions = db.Questions.Find(id); db.Questions.Remove(questions); db.SaveChanges(); return RedirectToAction("Index"); }

最满意答案

在delete image中将row id添加为data-id属性。 为它绑定一个单击处理程序。 on click删除图像,使用先前设置的data-id属性获取行id,并在AJAX请求中传递该id 。

码:

for (var i = 0; i <= result.length - 1; i++) { var editImage = '@Url.Content("~/Images/Edit.png")'; var deleteImage = '@Url.Content("~/Images/Delete.png")'; var obj = '<div style="margin-left: 25px; margin-top: 10px;>' + '<b style="margin-left: 10px;">' +(i + 1) + '<b style="margin-left:20px;">' + result[i].Email + '</b>'+ '<b>' + '<b style="margin-left: 20px;">' + result[i].Name + '</b>' + '<a style="float: right; margin-right: 20px;">' + '<img src="' + editImage + '">' + '</a>' + '<a style="float: right; margin-right: 20px;">' + '<img data-id=' + result[i].Id + ' src="' + deleteImage + '">' + ^ '</a>' + ^ '</div>'; ^ ^ ------------------------------ //set id as data-id attribute $("#email_list").append(obj); } //bind click handler $("#email_list").on("click", "img", function(){ var id = $(this).attr("data-id"); deleteRow(id); }); function deleteRow(Id) { alert('Delete email with id: ' + id); //your ajax call here... $.ajax({ //... }); }

工作演示

Add you row id as data-id attribute indelete image. Bind a click handler for it. on click of delete image, get your row id using data-id attribute that you previously set and pass that id in an AJAX request.

Code:

for (var i = 0; i <= result.length - 1; i++) { var editImage = '@Url.Content("~/Images/Edit.png")'; var deleteImage = '@Url.Content("~/Images/Delete.png")'; var obj = '<div style="margin-left: 25px; margin-top: 10px;>' + '<b style="margin-left: 10px;">' +(i + 1) + '<b style="margin-left:20px;">' + result[i].Email + '</b>'+ '<b>' + '<b style="margin-left: 20px;">' + result[i].Name + '</b>' + '<a style="float: right; margin-right: 20px;">' + '<img src="' + editImage + '">' + '</a>' + '<a style="float: right; margin-right: 20px;">' + '<img data-id=' + result[i].Id + ' src="' + deleteImage + '">' + ^ '</a>' + ^ '</div>'; ^ ^ ------------------------------ //set id as data-id attribute $("#email_list").append(obj); } //bind click handler $("#email_list").on("click", "img", function(){ var id = $(this).attr("data-id"); deleteRow(id); }); function deleteRow(Id) { alert('Delete email with id: ' + id); //your ajax call here... $.ajax({ //... }); }

Working DEMO

更多推荐

var,id,AJAX,return,电脑培训,计算机培训,IT培训"/> <meta name="descriptio