using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace Drag_Drop_Reorder_MVC.Controllers
{
    public class HomeController : Controller
    {
        // GET: Home
        public ActionResult Index()
        {
            HolidayLocationsEntities entities = new HolidayLocationsEntities();
            return View(entities.HolidayLocations.OrderBy(p => p.Preference).ToList());
        }

        [HttpPost]
        public ActionResult Index(int[] locationId)
        {
            HolidayLocationsEntities entities = new HolidayLocationsEntities();
            int preference = 1;
            foreach (int id in locationId)
            {
                var holidayLocation = entities.HolidayLocations.Find(id);
                holidayLocation.Preference = preference;
                entities.SaveChanges();
                preference += 1;
            }

            return View(entities.HolidayLocations.OrderBy(p => p.Preference).ToList());
        }
    }
}