// GET: Matching/Form/:id
 public ActionResult Form(int?id, string boxId, string screenId)
 {
     Models.MatchingDeviceVM model = null;
     if (id != null)
     {
         var device = this.deviceService.Get(id);
         if (device != null)
         {
             model = new Models.MatchingDeviceVM
             {
                 DeviceId    = device.DeviceID,
                 Title       = device.Title,
                 ScreenId    = device.ScreenID,
                 BoxId       = device.BoxID,
                 ScreenName  = screenService.GetScreenNameByID(device.ScreenID),
                 BoxName     = boxService.GetBoxNameByID(device.BoxID),
                 Description = device.Description
             };
         }
     }
     else
     {
         int boxID    = Int32.Parse(boxId);
         int screenID = Int32.Parse(screenId);
         model = new Models.MatchingDeviceVM
         {
             ScreenName = screenService.GetScreenNameByID(screenID),
             BoxName    = boxService.GetBoxNameByID(boxID),
             ScreenId   = screenID,
             BoxId      = boxID,
         };
     }
     return(View(model));
 }
        // GET: MatchingDevice/index
        public ActionResult Index()
        {
            var devices   = this.deviceService.Get().ToList();
            var deviceVMs = new List <Models.MatchingDeviceVM>();

            foreach (var item in devices)
            {
                var location       = locationService.Get(item.Screen.LocationID);
                var locationString = location.Address + ", Quận " + location.District + ", TP." + location.Province;
                var b = new Models.MatchingDeviceVM
                {
                    Location     = locationString,
                    DeviceId     = item.DeviceID,
                    Description  = item.Description,
                    BoxName      = boxService.GetBoxNameByID(item.BoxID),
                    ScreenName   = screenService.GetScreenNameByID(item.ScreenID),
                    Title        = item.Title,
                    MatchingCode = item.MatchingCode,
                    IsHorizontal = item.Screen.isHorizontal
                };
                deviceVMs.Add(b);
            }
            ViewBag.devicesList        = deviceVMs;
            ViewBag.locationStringList = GetLocationIdByBrandId();
            ViewBag.addSuccess         = Session["ADD_RESULT"] ?? false;
            ViewBag.updateSuccess      = Session["UPDATE_RESULT"] ?? false;
            Session.Clear();
            return(View());
        }
        public async System.Threading.Tasks.Task <ActionResult> Update(Models.MatchingDeviceVM model)
        {
            if (ModelState.IsValid)
            {
                var device = this.deviceService.Get(model.DeviceId);
                if (device != null)
                {
                    device.DeviceID    = (int)model.DeviceId;
                    device.BoxID       = model.BoxId;
                    device.ScreenID    = model.ScreenId;
                    device.Title       = model.Title;
                    device.Description = model.Description;
                }
                await this.deviceService.UpdateAsync(device);

                Session.Clear();
                Session["UPDATE_RESULT"] = true;
                return(new ContentResult
                {
                    Content = string.Format("<script type='text/javascript'>window.parent.location.href = '{0}';</script>", Url.Action("Index", "MatchingDevice")),
                    ContentType = "text/html"
                });
            }
            return(View("Form", model));
        }
        public async System.Threading.Tasks.Task <ActionResult> Add(Models.MatchingDeviceVM model)
        {
            DateTime aDateTime = DateTime.Now;

            if (ModelState.IsValid)
            {
                var randomString = RandomString(8);
                var device       = new Data.Models.Entities.Device
                {
                    CreateDatetime = aDateTime,
                    BoxID          = model.BoxId,
                    ScreenID       = model.ScreenId,
                    Title          = model.Title,
                    Description    = model.Description,
                    BrandID        = Helper.GetCurrentUser().BrandID,
                    MatchingCode   = randomString,
                };
                await this.deviceService.CreateAsync(device);

                //return this.RedirectToAction("Index");
                Session.Clear();
                Session["ADD_RESULT"] = true;
                return(new ContentResult
                {
                    Content = string.Format("<script type='text/javascript'>window.parent.location.href = '{0}';</script>", Url.Action("Index", "MatchingDevice")),
                    ContentType = "text/html"
                });
            }
            return(View("Form", model));
        }