Click here to Skip to main content
15,889,931 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
So these are the methods that handle picture upload in the controller .

[HttpPost]
        [ValidateAntiForgeryToken]
        public ActionResult Edit([Bind(Include = "AlbumId,GenreId,ArtistId,Title,Price,AlbumArtUrl")] Album album, HttpPostedFileBase file)
        {
            if (ModelState.IsValid)
            {
              
          
                if (file != null)
                {
                    file.SaveAs(HttpContext.Server.MapPath("/Content/Images/") + file.FileName);
                    album.AlbumArtUrl = ("/Content/Images/") + file.FileName;
                }
                db.Entry(album).State = EntityState.Modified;
                db.SaveChanges();
                return RedirectToAction("Index");
            }
            ViewBag.ArtistId = new SelectList(db.Artists, "ArtistId", "Name", album.ArtistId);
            ViewBag.GenreId = new SelectList(db.Genres, "GenreId", "Name", album.GenreId);
            return View(album);
        }





[HttpPost]
       [ValidateAntiForgeryToken]
       public ActionResult Create([Bind(Include = "AlbumId,GenreId,ArtistId,Title,Price,AlbumArtUrl")] Album album, HttpPostedFileBase file)
       {
           if (ModelState.IsValid)
           {
               if (file != null)
               {
                  file.SaveAs(HttpContext.Server.MapPath("/Content/Images/")+file.FileName);
                   album.AlbumArtUrl = ("/Content/Images/")+file.FileName;
               }


               db.Albums.Add(album);
               db.SaveChanges();
               return RedirectToAction("Index");
           }

           ViewBag.ArtistId = new SelectList(db.Artists, "ArtistId", "Name", album.ArtistId);
           ViewBag.GenreId = new SelectList(db.Genres, "GenreId", "Name", album.GenreId);
           return View(album);
       }




SO basicaly first lines of methods are the same , and what i want is to create a separate method called Upload , and call it inside Create() and Edit () . Is is possible , could anyone guide me or tell me how ? Thanks.

What I have tried:

I explained above the question.
Posted
Updated 21-Jun-17 17:52pm

1 solution

try

[HttpPost]
        [ValidateAntiForgeryToken]
        public ActionResult Edit([Bind(Include = "AlbumId,GenreId,ArtistId,Title,Price,AlbumArtUrl")] Album album, HttpPostedFileBase file)
        {
            return CreateEdit(file, album, false);
        }
        [HttpPost]
        [ValidateAntiForgeryToken]
        public ActionResult Create([Bind(Include = "AlbumId,GenreId,ArtistId,Title,Price,AlbumArtUrl")] Album album, HttpPostedFileBase file)
        {
            return CreateEdit(file, album, true);
        }

        private ActionResult CreateEdit(HttpPostedFileBase file,Album album,  bool isCreate)
        {
            if (ModelState.IsValid)
            { 
                if (file != null)
                {
                    file.SaveAs(HttpContext.Server.MapPath("/Content/Images/") + file.FileName);
                    album.AlbumArtUrl = ("/Content/Images/") + file.FileName;
                }
                if(isCreate)
                    db.Albums.Add(album);
                else
                db.Entry(album).State = EntityState.Modified;
                db.SaveChanges();
                return RedirectToAction("Index");
            }


            ViewBag.ArtistId = new SelectList(db.Artists, "ArtistId", "Name", album.ArtistId);
            ViewBag.GenreId = new SelectList(db.Genres, "GenreId", "Name", album.GenreId);
            return View(album);
        }
 
Share this answer
 
Comments
ddgjgj 22-Jun-17 3:35am    
wow.
Karthik_Mahalingam 22-Jun-17 23:57pm    
:)

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900