public async Task <ActionResult> Edit([Bind(Include = "ID,OfTypeID,Title,ImageFile,ImageSize,Description,LastEdit")] OfSubType ofSubType, HttpPostedFileBase imgFile)
        {
            if (ModelState.IsValid)
            {
                if (imgFile != null)
                {
                    string imgName   = Path.GetFileName(imgFile.FileName);
                    double imgSize   = imgFile.ContentLength;
                    string imgToPath = Path.Combine(Server.MapPath("~/Content/Uploads/SubTypes"), imgName);
                    if (!System.IO.File.Exists(imgToPath))
                    {
                        // Delete previously uploaded file
                        System.IO.File.Delete(ofSubType.ImageFile);
                        // Image file is uploaded
                        imgFile.SaveAs(imgToPath);
                        ofSubType.ImageFile = imgToPath;
                        // New file size
                        ofSubType.ImageSize = imgSize;
                    }
                }
                db.Entry(ofSubType).State = EntityState.Modified;
                await db.SaveChangesAsync();

                return(RedirectToAction("Index"));
            }
            ViewBag.LastEdit     = DateTime.Now;
            ViewBag.OfTypeIDList = new SelectList(db.OfTypes, "ID", "Title", ofSubType.OfTypeID);
            return(View(ofSubType));
        }
Exemple #2
0
        public async Task <IHttpActionResult> PutOfSubType(int id, OfSubType ofSubType)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

            if (id != ofSubType.ID)
            {
                return(BadRequest());
            }

            db.Entry(ofSubType).State = EntityState.Modified;

            try
            {
                await db.SaveChangesAsync();
            }
            catch (DbUpdateConcurrencyException)
            {
                if (!OfSubTypeExists(id))
                {
                    return(NotFound());
                }
                else
                {
                    throw;
                }
            }

            return(StatusCode(HttpStatusCode.NoContent));
        }
Exemple #3
0
        public async Task <IHttpActionResult> GetOfSubType(int id)
        {
            OfSubType ofSubType = await db.OfSubTypes.FindAsync(id);

            if (ofSubType == null)
            {
                return(NotFound());
            }

            return(Ok(ofSubType));
        }
Exemple #4
0
        public async Task <IHttpActionResult> PostOfSubType(OfSubType ofSubType)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

            db.OfSubTypes.Add(ofSubType);
            await db.SaveChangesAsync();

            return(CreatedAtRoute("DefaultApi", new { id = ofSubType.ID }, ofSubType));
        }
Exemple #5
0
        public async Task <IHttpActionResult> DeleteOfSubType(int id)
        {
            OfSubType ofSubType = await db.OfSubTypes.FindAsync(id);

            if (ofSubType == null)
            {
                return(NotFound());
            }

            db.OfSubTypes.Remove(ofSubType);
            await db.SaveChangesAsync();

            return(Ok(ofSubType));
        }
        // GET: OfSubTypes/Details/5
        public async Task <ActionResult> Details(int?id)
        {
            if (id == null)
            {
                return(new HttpStatusCodeResult(HttpStatusCode.BadRequest));
            }
            OfSubType ofSubType = await db.OfSubTypes.FindAsync(id);

            if (ofSubType == null)
            {
                return(HttpNotFound());
            }
            return(View(ofSubType));
        }
        public async Task <ActionResult> DeleteConfirmed(int id)
        {
            OfSubType ofSubType = await db.OfSubTypes.FindAsync(id);

            db.OfSubTypes.Remove(ofSubType);
            await db.SaveChangesAsync();

            // Check for associated image file if exists then delete it
            if (System.IO.File.Exists(ofSubType.ImageFile))
            {
                System.IO.File.Delete(ofSubType.ImageFile);
            }
            return(RedirectToAction("Index"));
        }
        // GET: OfSubTypes/Edit/5
        public async Task <ActionResult> Edit(int?id)
        {
            if (id == null)
            {
                return(new HttpStatusCodeResult(HttpStatusCode.BadRequest));
            }
            OfSubType ofSubType = await db.OfSubTypes.FindAsync(id);

            if (ofSubType == null)
            {
                return(HttpNotFound());
            }
            ViewBag.LastEdit     = DateTime.Now;
            ViewBag.OfTypeIDList = new SelectList(db.OfTypes, "ID", "Title", ofSubType.OfTypeID);
            return(View(ofSubType));
        }
        public async Task <ActionResult> Create([Bind(Include = "ID,OfTypeID,Title,ImageFile,ImageSize,Description,LastEdit")] OfSubType ofSubType, HttpPostedFileBase imgFile)
        {
            if (ModelState.IsValid)
            {
                if (imgFile != null)
                {
                    string imgName   = Path.GetFileName(imgFile.FileName);
                    double imgSize   = imgFile.ContentLength;
                    string imgToPath = Path.Combine(Server.MapPath("~/Content/Uploads/SubTypes"), imgName);
                    // Image file is uploaded
                    imgFile.SaveAs(imgToPath);
                    ofSubType.ImageFile = imgToPath;
                    // New file size
                    ofSubType.ImageSize = imgSize;
                }
                db.OfSubTypes.Add(ofSubType);
                await db.SaveChangesAsync();

                return(RedirectToAction("Index"));
            }

            ViewBag.OfTypeIDList = new SelectList(db.OfTypes, "ID", "Title", ofSubType.OfTypeID);
            return(View(ofSubType));
        }