Example #1
0
        // GET: /Measure/Details/5
        public ActionResult Details(int?id, string ReturnUrl)
        {
            if (id == null)
            {
                return(new HttpStatusCodeResult(HttpStatusCode.BadRequest));
            }
            MeasureModel model = new MeasureModel();

            model.measure = (from measure in db.Measures
                             .Include(ma => ma.Materials.Select(r => r.Rooms))
                             .Include(ma => ma.Materials.Select(mt => mt.MaterialType))
                             .Include(s => s.Store)
                             where measure.Id == id.Value
                             select measure).FirstOrDefault();
            if (model.measure == null)
            {
                return(HttpNotFound());
            }
            model.customer = db.Customers.Find(model.measure.CustomerId);
            if (model.customer == null)
            {
                return(HttpNotFound());
            }
            model.CustomerId = model.customer.Id;
            model.MeasureId  = model.measure.Id;

            model.Rooms = new List <RoomModel>();
            foreach (Room r in db.Rooms.OrderBy(r => r.Name))
            {
                RoomModel room = new RoomModel();
                room.RoomId     = r.Id;
                room.Name       = r.Name;
                room.ShowCloset = r.ShowCloset;
                foreach (MeasureMaterial mm in model.measure.Materials)
                {
                    MeasureRoom mr = mm.Rooms.FirstOrDefault(mr2 => mr2.RoomId == r.Id);
                    if (mr != null)
                    {
//						room.Name = mr.Name;
                        room.MaterialId    = mr.MaterialId;
                        room.IncludeCloset = mr.IncludeCloset;
                        break;
                    }
                }

                model.Rooms.Add(room);
            }
            ViewBag.ReturnUrl   = ReturnUrl;
            ViewBag.ShowDeleted = true;

            ViewBag.MaterialList = InsertEmptyFirst(new SelectList(model.measure.Materials, "Id", "Description", null));
            return(View(model));
        }
Example #2
0
        public ActionResult Details(MeasureModel model)
        {
            if (ModelState.IsValid)
            {
                Measure m = (from measure in db.Measures
                             .Include(ma => ma.Materials.Select(r => r.Rooms))
                             .Include(ma => ma.Materials.Select(mt => mt.MaterialType))
                             .Include(s => s.Store)
                             where measure.Id == model.MeasureId select measure).FirstOrDefault();
                if (m == null)
                {
                    return(new HttpStatusCodeResult(HttpStatusCode.BadRequest));
                }
                List <Room> Rooms = db.Rooms.ToList();

                foreach (MeasureMaterial mm in m.Materials)
                {
                    var rooms    = new HashSet <int> (mm.Rooms.Select(c => c.RoomId));
                    var newRooms = new HashSet <int>(model.Rooms.Where(z => z.MaterialId == mm.Id).Select(z => z.RoomId));

                    foreach (var room in Rooms)
                    {
                        if (newRooms.Contains(room.Id))
                        {
                            RoomModel rm = model.Rooms.First(r => r.RoomId == room.Id);
                            if (!rooms.Contains(room.Id))
                            {
                                mm.Rooms.Add(new MeasureRoom()
                                {
                                    RoomId = room.Id, IncludeCloset = rm.IncludeCloset
                                });
                            }
                            else
                            {
                                MeasureRoom mmr = mm.Rooms.First(r => r.RoomId == room.Id);
                                mmr.IncludeCloset = rm.IncludeCloset;
                            }
                        }
                        else
                        {
                            if (rooms.Contains(room.Id))
                            {
                                MeasureRoom mr = mm.Rooms.FirstOrDefault(z => z.RoomId == room.Id);
//								mm.Rooms.Remove(mr);
                                db.MeasureRooms.Remove(mr);
                            }
                        }
                    }
                }
                int change = db.SaveChanges();
                return(RedirectToAction("Details", "Customer", new { id = model.CustomerId }));
            }
            model            = new MeasureModel();
            model.measure    = db.Measures.Find(model.MeasureId);
            model.customer   = db.Customers.Find(model.measure.CustomerId);
            model.CustomerId = model.customer.Id;
            model.MeasureId  = model.measure.Id;

            ViewBag.MaterialList = InsertEmptyFirst(new SelectList(model.measure.Materials, "Id", "Description", null));
            return(View(model));
        }