Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8793087
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T23:04:41+00:00 2026-06-13T23:04:41+00:00

I have action method in my controller. If photo is attached in form my

  • 0

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?

Model:

[DisplayColumn("LastName")]
public class Driver
{
    [Key]
    public int Id { get; set; }

    [Display(Name="Full Name")]
    [DataType(DataType.Text)]
    public string FullName
    {
        get { return string.Concat(FirstName.Substring(0, 1), ".", LastName);}
    }

    [Required]
    [Display(Name = "First Name")]
    [DataType(DataType.Text)]
    public string FirstName { get; set; }

    [Display(Name = "Middle Name")]
    [DataType(DataType.Text)]
    public string MiddleName { get; set; }

    [Required]
    [Display(Name = "Last Name")]
    [DataType(DataType.Text)]
    public string LastName { get; set; }

    public byte[] DriverPhoto { get; set; }

}

Controller:

    [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);
    }
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-13T23:04:42+00:00Added an answer on June 13, 2026 at 11:04 pm

    I modified code my controller this:

            [HttpPost]
        public ActionResult Edit(Driver driver, HttpPostedFileBase fileUpload)
        {
            if (fileUpload != null)
            {
                if (fileUpload.ContentType.ToLower() == "image/jpeg")
                {
                    var binaryReader = new BinaryReader(fileUpload.InputStream);
                    driver.DriverPhoto = binaryReader.ReadBytes(fileUpload.ContentLength);
                }
                else
                {
                    ModelState.AddModelError("DriverPhoto","Illustrations should be uploaded in JPEG format.");
                }
        }
    
            if (ModelState.IsValid)
            {
                var entry = _db.Entry(driver);
    
                entry.State = EntityState.Modified;
    
                if (fileUpload == null)
                {
                    entry.Property(t => t.DriverPhoto).IsModified = false;
                }
    
                _db.SaveChanges();
                return RedirectToAction("Index");
            }
    
            return View(driver);
        }
    

    Now my controller works the way I wanted, but I’m not sure about the correctness of the solution.

    Thanks for help me.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have an action-method in a controller that takes requests coming from a variety
I have a requirement to open a popup from an action method in controller.
Here is a controller action method that I have to upload a user's profile
I have a controller action.How can i call action method from another controller in
I have an Action method in an ASP.NET MVC Controller class that handles form
I have the following action method in my Tournament controller: public ActionResult Edit(int id)
I have the following code in my controller action method: if (User.Identity.IsAuthenticated) { //
I have a Rails controller action to test. In that action, a method User.can?
I have an action method returning a JsonResult in my controller: public JsonResult GetDetails()
Do I have to do anything special to my controller action method to accept/bind

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.