Esempio n. 1
0
        public ActionResult IndexBySite(string Id)
        {
            var    model            = db.AspNetUsers.Where(s => s.Id == Id).FirstOrDefault();
            string selectedusername = model.Email;

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

            authorisedlist             = authsitemodel.Select(s => s.ID).ToArray();
            ViewData["UnSelectedList"] = GetSiteItems();
            SiteAuthModel siteauthmodel = new SiteAuthModel()
            {
                Id = Id,
                SelectedUserName = selectedusername,
                Authorised       = authorisedlist
            };

            return(View(siteauthmodel));
        }
Esempio n. 2
0
        public ActionResult IndexBySite(SiteAuthModel model)
        {
            int[] selectedItems = CheckBoxListExtension.GetSelectedValues <int>("checkBoxList1");

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

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

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

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

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

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

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

            return(View(model));
        }