Пример #1
0
        public void Create(Subject subject)
        {
            if (string.IsNullOrWhiteSpace(subject.Subject_Name))
            {
                throw new AppException("Subject Name is required");
            }

            if (_context.Subject.Any(x => x.Subject_Name == subject.Subject_Name))
            {
                throw new AppException("Subject " + subject.Subject_Name + " already exists");
            }

            _context.Subject.Add(subject);
            _context.SaveChanges();
        }
Пример #2
0
        public ActionResult Create(SubjectModel sub, HttpPostedFileBase uploadImage)
        {
            if (ModelState.IsValid && uploadImage != null)
            {
                byte[] imageData = null;
                // считываем переданный файл в массив байтов
                using (var binaryReader = new BinaryReader(uploadImage.InputStream))
                {
                    imageData = binaryReader.ReadBytes(uploadImage.ContentLength);
                }
                // установка массива байтов
                sub.Image = imageData;

                db.Subjects.Add(sub);
                db.SaveChanges();

                return(RedirectToAction("Index"));
            }
            return(View(sub));
        }
Пример #3
0
        public ActionResult Create(Subject subject)
        {
            try
            {
                if (ModelState.IsValid)
                {
                    db.Subjects.Add(subject);
                    db.SaveChanges();
                    return(RedirectToAction("Index"));
                }

                return(View(subject));
            }
            catch
            {
                return(View());
            }
        }