locked
Update Data using Ajax Javascript/Jquery RRS feed

  • Question

  • User2041008840 posted

    I create function in controller 

    and want to update the record after drag and drop 
    I dont know how to code it in js please help me, send me code

    $(function () {
               $(".deal-card").draggable({ helper: 'clone' });
     
               $(".stage-container").droppable({
                   accept: ".deal-card",
                   drop: function (evui) {
                       var droppedItem = $(ui.draggable); //.clone()
                       var targetid = $(ev.target).attr('id');
                       var dealid = ui.draggable.attr('id');
     
                     //want update data in database code -  UpdateDeals Function here to run
     
                       alert(ui.draggable.attr('id'));
     
                       alert(targetid);
                       $(this).append(droppedItem);
    
    [HttpPut]
            public ActionResult UpdateDeal(int StageIDint DealID)
            {
     
                Deal DealData = _context.Deal.Where(x => x.ID == DealID).FirstOrDefault();
     
                if (DealData != null)
                {
                    DealData.StageID = StageID;
                    _context.Entry(DealData).State = EntityState.Modified;
                }
     
                _context.SaveChanges();
                return View();
                //return Json(query); 
            }
    Thursday, October 24, 2019 4:42 PM

Answers

  • User665608656 posted

    Hi Prathamesh,

    According to your description, I suggest you httppost to implement the method operation of ajax in jquery.

    Here is the code as follows:

        $(function () {
                $(".deal-card").draggable({ helper: 'clone' });
                 $(".stage-container").droppable({
                    accept: ".deal-card",
                    drop: function (ev, ui) {
                        var droppedItem = $(ui.draggable);//.clone();
                        var targetid = $(ev.target).attr('dataId');
                        var dealid = ui.draggable.attr('id'); 
                        $.ajax({
                            type: 'Post',
                            url: "/YourControllerName/UpdateDeal",
                            data: { StageID: targetid, DealID: dealid },
                            success: function () {
                                alert("update successfully!");
    $(this).append(droppedItem); } }); } }); });
    [HttpPost]
            public ActionResult UpdateDeal(int StageID, int DealID)
            {
     
                Deal DealData = _context.Deal.Where(x => x.ID == DealID).FirstOrDefault();
     
                if (DealData != null)
                {
                    DealData.StageID = StageID;
                    _context.Entry(DealData).State = EntityState.Modified;
                }
     
                _context.SaveChanges();
                return View();
                //return Json(query); 
            }

    Best Regards,

    YongQing.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, October 25, 2019 7:44 AM