Skip to content

Move up/down record with LINQ

albert albert

Asked Jul 3, 2014, 9:12 PM EDT2 answers
Share
LinkedInX
Hi everybody,
 
I  have 2 buttons: UP and Down.
 
And if you press up , then the record has to goes up and for down - the record has to go down.
 
This is the ajax call: 
[code]
 
$.ajax({
type: 'POST',
url:Medicijnverstrekking.baseUrl + '/Medicijn/MoveUp/',
dataType: 'json',
data:{Id: dataItem.Id},
JavaScript
                    success: function (result) {
                        //$("#medicijnenGridVolgorde").data("kendoGrid").dataSource.data(result);

                        //if (newIndex != index) {

                            //grid.dataSource.remove(dataItem);
                            //grid.dataSource.insert(newIndex, dataItem);
                            //$("#medicijnenGridVolgorde").data("kendoGrid").dataSource.fetch();

                           // grid.dataSource.read();
                            grid.dataSource.refresh();
                        //}
                    }
                })

                return false;
            }
[code] 
 
and this is the method in the controller:
 
[code]
 
Java
 public ActionResult MoveUP(int id /*MedicijnViewModel model, HttpPostedFileBase bijsluiter*/)
        {
            var medicijn = medicijnService.GetById(id);


            Item item = medicijnService.GetById(id).  medicijn.Id. .Items.Single(i => i.Id == id);

            if (medicijn.Id > 0)
            {
                medicijn predecessor = Context.Items.Single(i => i.Index == medicijn.Id - 1);

                item.Index -= 1;
                predecessor.Index += 1;

                //predecessor;

                  medicijnService.Save(medicijn);

                //Context.SaveChanges();
            }

            return View();

        }
[/code] 
 
Thank you for your help. 

2 Answers

Know the answer? Post it. Somebody with the same question will find it here.

  • 0
    i try something like this:

    C#
     public ActionResult MoveUP(int id /*MedicijnViewModel model, HttpPostedFileBase bijsluiter*/)
            {
                var medicijn = medicijnService.GetById(id);
                var medicijnTemp = medicijnService.GetById(id);
    
    
    
    
                //Item item = medicijnService.GetById(id).  medicijn.Id.Items.Single(i => i.Id == id);
                medicijn = medicijnRepository.Query.Single(i => i.Id == id);
    
    
                if (medicijn.Id > 0)
                {
                    //medicijn predecessor = Context.Items.Single(i => i.Index == medicijn.Id - 1);
                    medicijnTemp = medicijnRepository.Query.Single(m => m.Id == medicijn.Id);
    
    
                    medicijn.Id -= 1;
                    medicijnService.GetById(id += 1);
    
    
                    medicijnService.Save(medicijn);
                }
    
    
                return View();
    
    
            }
    0
  • 0
    I have it now like this:

    [code]
    C#
    public ActionResult MoveUP(int id /*MedicijnViewModel model, HttpPostedFileBase bijsluiter*/)
            {
                var medicijn = medicijnService.GetById(id);
                var medicijnTemp = medicijnService.GetById(id);
    
    
    
    
                //Item item = medicijnService.GetById(id).  medicijn.Id.Items.Single(i => i.Id == id);
                medicijn = medicijnRepository.Query.Single(i => i.Id == id);
    
    
                if (medicijn.Id > 0)
                {
                    //medicijn predecessor = Context.Items.Single(i => i.Index == medicijn.Id - 1);
                    medicijnTemp = medicijnRepository.Query.Single(m => m.Id == medicijn.Id);
    
    
                    medicijn.Id -= 1;
                    medicijnTemp.Id += 1;
                    //medicijnService.GetById(id += 1);
    
    
                    medicijnService.Save(medicijn);
                }
    
    
                return View();
    
    
            }
    [/code]

    But if I look in this:

    medicijn.Id -= 1;
                    medicijnTemp.Id += 1;

    Then I see they have the same value.
    0