Beispiel #1
0
        //[ValidateAntiForgeryToken]
        public async Task <ActionResult> Edit(int?id)
        {
            try
            {
                if (id == null)
                {
                    //return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
                    return(PartialView("IndexNotFound", id.ToString()));
                }
                Book book = await db.Books.FindAsync(id);

                if (book == null)
                {
                    //return HttpNotFound();
                    return(PartialView("IndexNotFound", id.ToString()));
                }

                var modelBook = BookRelase.EditBook(book);
                ViewBag.AuthorsId          = new SelectList(db.Authors, "Id", "FullName", book.AuthorsId);
                ViewBag.CountryPublishedId = new SelectList(db.CountryPublisheds, "Id", "CountryName", book.CountryPublishedId);
                return(View(modelBook));
            }
            catch
            {
                return(RedirectToAction("Index"));
            }
        }
Beispiel #2
0
        public async Task <ActionResult> Edit([Bind(Include = "Id,Title,Price,Description,PagesCount,Picture,ImagePatchs,CountryPublishedId,AuthorsId")] BookViewModel model, HttpPostedFileBase upload)
        {
            try
            {
                if (ModelState.IsValid)
                {
                    //book = await db.Books.FindAsync(book.Id);
                    ImagePatch oldpatch = db.ImagePatchs.Where(n => n.BooksId == model.Id).FirstOrDefault();
                    string     pat      = oldpatch.ImageUrl;
                    if (upload != null)
                    {
                        var supportedTypes = new[] { "jpg", "jpeg", "png" };
                        var fileExt        = System.IO.Path.GetExtension(upload.FileName).Substring(1);

                        if (!supportedTypes.Contains(fileExt))
                        {
                            return(RedirectToAction("Index"));
                            // ModelState.AddModelError("photo", "Invalid type. Only the following types (jpg, jpeg, png) are supported.");
                        }

                        string filename = Guid.NewGuid().ToString() + Path.GetExtension(upload.FileName);
                        string patch    = Path.Combine(Server.MapPath("~/Images"), filename);
                        upload.SaveAs(patch);

                        ImagePatch patchimg = db.ImagePatchs.Where(n => n.BooksId == model.Id).FirstOrDefault();
                        if (patchimg != null)
                        {
                            patchimg.ImageUrl = filename;

                            string patch1 = Path.Combine(Server.MapPath("~/Images"), pat);

                            if (System.IO.File.Exists(patch1))
                            {
                                System.IO.File.Delete(patch1);
                            }
                        }
                    }

                    Book b = BookRelase.EditBook(model);
                    db.Entry(b).State = EntityState.Modified;
                    await db.SaveChangesAsync();

                    return(RedirectToAction("Index"));
                }
                ViewBag.AuthorsId          = new SelectList(db.Authors, "Id", "FullName", model.AuthorsId);
                ViewBag.CountryPublishedId = new SelectList(db.CountryPublisheds, "Id", "CountryName", model.CountryPublishedId);
                return(View(model));
            }
            catch
            {
                return(RedirectToAction("Index"));
            }
        }