none
How to save oldest photo if in form not attached newest file? RRS feed

  • Question

  • I have action method in my controller. If photo is attached in form my method work fine. If photo don't upload from form in Edit action my method to removes oldest photo from database. How to save oldest photo if in form not attached newest file?

    [HttpPost]
        public ActionResult Edit(Driver driver, HttpPostedFileBase fileUpload)
        {
            if(fileUpload != null)
            {
                var binaryReader = new BinaryReader(fileUpload.InputStream);
                driver.DriverPhoto = binaryReader.ReadBytes(fileUpload.ContentLength);
            }
    
            if (ModelState.IsValid)
            {
    
                db.Entry(driver).State = EntityState.Modified;
    
                db.SaveChanges();
                return RedirectToAction("Index");
            }
    
            return View(driver);
        }

    Thanks for help me.
    • Edited by Roman J. Barulin Saturday, November 3, 2012 5:59 PM Thanks for help me.
    Saturday, November 3, 2012 5:59 PM

Answers

  • Hi Roman,

    Welcome to the MSDN forum.

    I am not sure if this works. Please check it:

        public ActionResult Edit(Driver driver, HttpPostedFileBase fileUpload)
        {
            if(fileUpload != null)
            {
                var binaryReader = new BinaryReader(fileUpload.InputStream);
                driver.DriverPhoto = binaryReader.ReadBytes(fileUpload.ContentLength);
    
                if (ModelState.IsValid)
                {
    
                    db.Entry(driver).State = EntityState.Modified;
    
                	db.SaveChanges();
                	return RedirectToAction("Index");
                }
            }
    
            return View(driver);
        }

    Best Regards,


    Alexander Sun [MSFT]
    MSDN Community Support | Feedback to us

    • Marked as answer by Alexander Sun Thursday, November 22, 2012 6:40 AM
    Monday, November 5, 2012 7:40 AM