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

  • Home
  • SEARCH
  • 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 6581033
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T16:06:06+00:00 2026-05-25T16:06:06+00:00

I’ve got a View that contains code that allows the user to upload images.

  • 0

I’ve got a View that contains code that allows the user to upload images. I pulled the code from a site that I can’t seem to find. The code works great, but my users have asked to see a preview of the image before submitting the page.

My View has the following for my image and upload button.

<div class="editor-field">
     @if (Model.ImageData == null || Model.ImageData.Length == 0) { 
         @:None
     }
     else { 
         <img width="150" height="150" alt="Item Image" src="@Url.Action("GetImage", "ItemManagement")" />
     }
  <div>Upload new image: <input type="file" name="Image" /></div>
</div>

My BeginForm helper looks like this.

@using(Html.BeginForm("Add", "ItemManagement", FormMethod.Post, new{enctype="multipart/form-data"})) {

So, when my view is posted back, my Add Action in my contoller looks like this.

    [HttpPost]
    public ActionResult Add(ItemAddModel model, HttpPostedFileBase image) {
        if (!ModelState.IsValid) {
            return View(model);
        }

        if (image != null) {
            model.ImageMimeType = image.ContentType;
            model.ImageData = new byte[image.ContentLength];
            image.InputStream.Read(model.ImageData, 0, image.ContentLength);
        }
        _itemService.AddItem(model);
        return RedirectToAction("Index");
    }

Like I said everything here works great. But it’s only once the form is submitted and my user opens the page again, can they see the image they’ve uploaded. I’d like to add an Update button, that will get the image and load it into the image control without posting back to the server. I don’t want to store the image in the database until the form is Submitted.

  • 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-05-25T16:06:07+00:00Added an answer on May 25, 2026 at 4:06 pm

    Here is the code in my view. The important parts of this are the submit button’s name being set to ‘UploadImageButton’. This matches a property in my ViewModel which is used by my Action method in my controller. Also, the class=”style-name cancel” keeps my Client side validation from firing.

    <div class="editor-field">
        @if (Model.ImageData == null || Model.ImageData.Length == 0) { 
            @:None
        }
        else { 
            <img id="itemImage" width="150" height="150" alt="@Model.ItemName" src="@Url.Action("GetImage", "ItemManagement", new { Model.ItemID })" />
        }
        @FileUpload.GetHtml(initialNumberOfFiles: 1, allowMoreFilesToBeAdded: false, includeFormTag: false, addText: "Add Image", uploadText: "Upload Image") <br />  
        <input type="submit" name="UploadImageButton" value="Upload Image" class="style-name cancel" />      
    </div>
    

    In my controller I have this code to handle HTTP Posts.

    [HttpPost]
    public ActionResult Edit(ItemEditModel model) {
        //Determine which button caused our postback
        if (!model.UploadImageButton.IsNullOrEmpty()) {
            //The user is uploading an image, so we don't need it check validation or update the database.
            //Clear our errors here so we don't show the user any validation issues. That will happen when the Save button is clicked.
            foreach (var key in modelState.Keys.ToList().Where(key => modelState.Keys.Contains(key))) {
                modelState[key].Errors.Clear();
            }
            var image = WebImage.GetImageFromRequest(); //Get the image from the request
    
            Session[ItemImageSessionKey] = image; //Save our image to the session 
    
            LoadAttributesIntoViewBagForEditView(model);
    
            return View("Edit", model); //Return our view and model with the users edits (if any)
        }
        //Normal code to handle Form submit (Save) functionality.
    }
    

    This now allows my users to Upload an image, which does a postback, but doesn’t fire any validation an keeps their other edits in the state they were at the time of the upload.

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

Sidebar

Related Questions

I've got a string that has curly quotes in it. I'd like to replace
I have a French site that I want to parse, but am running into
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
Does anyone know how can I replace this 2 symbol below from the string
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
For some reason, after submitting a string like this Jack’s Spindle from a text
I have a jquery bug and I've been looking for hours now, I can't
I am currently running into a problem where an element is coming back from
I have this code: - (void)parser:(NSXMLParser *)parser foundCDATA:(NSData *)CDATABlock { NSString *someString = [[NSString

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.