0

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");
    }
7
  • so, what exactly is problem you're facing? Commented Apr 14, 2017 at 7:10
  • How I need to write AJAX request for delete? @SyedAliTaqi Commented Apr 14, 2017 at 7:14
  • you have defined a post method inside your controller. just make an AJAX call with method as post and pass id of row with url. Commented Apr 14, 2017 at 7:40
  • What stops you from copy/pasting the ajax method you have, and changing it so that it would call delete method? Commented Apr 14, 2017 at 7:48
  • @Eugene: are you having difficulty in binding click handler or getting the id on click? Commented Apr 14, 2017 at 7:54

1 Answer 1

1

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

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.