Exemplo n.º 1
0
        public ActionResult Create([Bind(Include = "Id,Text,Date")] Homework homework, HttpPostedFileBase[] fileUpload)
        {
            if (ModelState.IsValid)
            {
                // Сначала важно вставить запись самого задания, только потом фото, т.к. оно ссылается на задание
                homework.Date = DateTime.Now;
                db.Homeworks.Add(homework);

                // Сохраняем файл в папку----------------------------------------------------
                foreach (var file in fileUpload)
                {
                    if (file == null) continue;
                    var path = AppDomain.CurrentDomain.BaseDirectory + "UploadedFiles/";
                    if (!Directory.Exists(path))
                    {
                        Directory.CreateDirectory(path);
                    }

                    var filename = Path.GetFileName(file.FileName);
                    if (filename != null) file.SaveAs(Path.Combine(path, filename));

                    var newPhoto = new HomeworksPhoto
                    {
                        Name = file.FileName,
                        HomeworksPhotoPath = "/UploadedFiles/" + filename,   // Нам не нужен путь на диске! Только относительный путь на сайте
                        HomeworkId = homework.Id
                    };
                    db.HomeworksPhotoList.Add(newPhoto);
                }
                // --------------------------------------------------------------------------

                db.SaveChanges();
                return RedirectToAction("Index");
            }

            return View(homework);
        }
Exemplo n.º 2
0
        public ActionResult Edit([Bind(Include = "Id,Text,Date")] Homework homework, string[] DeleteCheckBox, HttpPostedFileBase[] fileUpload)
        {
            if (ModelState.IsValid)
            {
                if (DeleteCheckBox != null && DeleteCheckBox.Length > 0)
                {
                    foreach (string DelPhoto in DeleteCheckBox)
                    {
                        HomeworksPhoto photo = db.HomeworksPhotoList.Find(Convert.ToInt32(DelPhoto));
                        if (System.IO.File.Exists(AppDomain.CurrentDomain.BaseDirectory + photo.HomeworksPhotoPath))
                        {
                            System.IO.File.Delete(AppDomain.CurrentDomain.BaseDirectory + photo.HomeworksPhotoPath);
                        }
                        db.HomeworksPhotoList.Remove(photo);
                    }
                }
                // Сохраняем файл в папку----------------------------------------------------
                foreach (var file in fileUpload)
                {
                    if (file == null) continue;
                    var path = AppDomain.CurrentDomain.BaseDirectory + "UploadedFiles/";
                    if (!Directory.Exists(path))
                    {
                        Directory.CreateDirectory(path);
                    }

                    var filename = Path.GetFileName(file.FileName);
                    if (filename != null) file.SaveAs(Path.Combine(path, filename));

                    var newPhoto = new HomeworksPhoto
                    {
                        Name = file.FileName,
                        HomeworksPhotoPath = "/UploadedFiles/" + filename,   // Нам не нужен путь на диске! Только относительный путь на сайте
                        HomeworkId = homework.Id
                    };
                    db.HomeworksPhotoList.Add(newPhoto);
                }
                // --------------------------------------------------------------------------

                db.Entry(homework).State = EntityState.Modified;
                db.SaveChanges();
                return RedirectToAction("Index");
            }
            return View(homework);
        }