示例#1
0
        public ActionResult IndexByPlant(string Id)
        {
            var    model            = db.AspNetUsers.Where(s => s.Id == Id).FirstOrDefault();
            string selectedusername = model.Email;

            int[] authorisedlist = new int[] { };
            var   authplantmodel = (from a in db.t_Hovis_Milling_User_Plant_Authorisation
                                    where a.UserName == selectedusername
                                    select new CheckBoxListItemObject
            {
                ID = (int)a.PlantRecid,
                Name = a.UserName
            }).ToList();

            authorisedlist             = authplantmodel.Select(s => s.ID).ToArray();
            ViewData["UnSelectedList"] = GetPlantItems();
            PlantAuthModel plantauthmodel = new PlantAuthModel()
            {
                Id = Id,
                SelectedUserName = selectedusername,
                Authorised       = authorisedlist
            };

            return(View(plantauthmodel));
        }
示例#2
0
        public ActionResult IndexByPlant(PlantAuthModel model)
        {
            int[] selectedItems = CheckBoxListExtension.GetSelectedValues <int>("checkBoxList2");

            //firstly lets remove any that are not in the list
            var currentmodel = db.t_Hovis_Milling_User_Plant_Authorisation;

            if (ModelState.IsValid)
            {
                try
                {
                    var currentauthlist = db.t_Hovis_Milling_User_Plant_Authorisation.Where(s => s.UserName == model.SelectedUserName).ToList();
                    if (currentauthlist != null)
                    {
                        var tobedeleted = currentauthlist.Where(p => !selectedItems.Any(p2 => p2 == p.PlantRecid)).ToList();

                        if (tobedeleted != null)
                        {
                            foreach (var trash in tobedeleted)
                            {
                                currentmodel.Remove(trash);
                                db.SaveChanges();
                            }
                        }
                        ;

                        var alreadyexist = currentauthlist.Where(q => selectedItems.Any(q2 => q2 == q.PlantRecid)).ToList();

                        var newrecords = selectedItems.Where(t => !alreadyexist.Any(t2 => t2.PlantRecid == t)).ToList();

                        if (newrecords != null)
                        {
                            foreach (var newitem in newrecords)
                            {
                                t_Hovis_Milling_User_Plant_Authorisation newauthrec = new t_Hovis_Milling_User_Plant_Authorisation();
                                newauthrec.PlantRecid = newitem;
                                newauthrec.UserName   = model.SelectedUserName;
                                currentmodel.Add(newauthrec);
                                db.SaveChanges();
                            }
                        }
                        ;

                        return(RedirectToAction("IndexList", "ApplicationUser"));
                    }
                }
                catch (Exception e)
                {
                    ViewData["EditError"] = e.Message;
                }
            }

            return(View(model));
        }