Exemplo n.º 1
0
        public ActionResult editLocation(int locationID, int schoolID, string description, Location location, string callback, HttpPostedFileWrapper image)
        {
            ajaxReturnData data = new ajaxReturnData();

            try
            {
                using (ApplicationDbContext DB = new ApplicationDbContext())
                {
                    location.id = locationID;

                    DB.Locations.Attach(location);
                    DB.Entry(location).State = EntityState.Unchanged;
                    DB.Entry(location).Property(l => l.name).IsModified = true;
                    //DB.Entry(location).Property(l => l.city).IsModified = true;
                    location.addContent(DB, "description", description);


                    if (image != null && image.ContentLength > 0)
                    {

                        string path = "/content/images/uploads/locations/" + locationID;
                        //bool existsfirst = System.IO.Directory.Exists(Server.MapPath(path));
                        bool exists = System.IO.Directory.Exists(Server.MapPath(path));
                        if (!exists)
                        {
                            Directory.CreateDirectory(Server.MapPath(path));
                        }

                        path = path + "/locationImage" + Path.GetExtension(image.FileName);
                        image.SaveAs(Server.MapPath(path));
                        location.image = path;
                        DB.Entry(location).Property(l => l.image).IsModified = true;
                    }
                    DB.SaveChanges();

                    School school = new School();
                    school.id = schoolID;
                    //school.schoolLocation = location;
                    school.locationID = location.id;
                    school.features = new SchoolFeatures();
                    DB.Schools.Attach(school);
                    DB.Entry(school).State = EntityState.Unchanged;
                    DB.Entry(school).Property(s => s.locationID).IsModified = true;

                    DB.SaveChanges();

                    


                }


                if (string.IsNullOrEmpty(callback))
                {
                    data.statusCode = (int)statusCodes.success;
                }
                else
                {
                    data.statusCode = (int)statusCodes.successRun;
                    data.callback = callback;
                }

                data.message = "location updated";
                return Json(data);
            }
            catch (Exception ex)
            {
                data.statusCode = (int)statusCodes.fail;
                data.message = "Failed to update location; " + ex.Message;
                return Json(data);
            }
        }