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 6558989
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T13:16:03+00:00 2026-05-25T13:16:03+00:00

I have a FileUpload (from microsoft.web.helpers) that is always null in the Edit ActionResult

  • 0

I have a FileUpload (from microsoft.web.helpers) that is always null in the Edit ActionResult method. In the Create method, it works fine. Only difference is that its a different model in Edit, where I access the original properties through that. (if that makes sense). Here the code so you can see. First the models and then the controllers:

    public class Files
{
    public int Id { get; set; }
    public List<string> FileName { get; set; }

    public Artikel Artikel { get; set; }
}

public class Artikel
{
    public int Id { get; set; }
    public string Headline { get; set; }
    public string Text { get; set; }]
    public DateTime Date { get; set; }
    public string Author { get; set; }
    public bool Proof { get; set; }
    public IEnumerable<HttpPostedFileBase> FileUpload { get; set; }
    public string TextShort { get { return Text.Substring(0, 50); } }
}

HttpPost create:

[HttpPost]
        public ActionResult Create(Artikel artikel)
        {
            if (ModelState.IsValid)
            {
                artikel.Date = DateTime.Now;
                db.Artikler.Add(artikel);
                db.SaveChanges();

            foreach (var file in artikel.FileUpload)
            {
                try
                {
                    if (file.ContentLength > 0)
                    {
                        var folder = Server.MapPath("~/uploads/Artikler/" + artikel.Id.ToString());
                        if (!Directory.Exists(folder))
                        {
                            Directory.CreateDirectory(folder);
                        }

                        var fileName = Path.GetFileName(file.FileName);
                        var path = Path.Combine(folder, fileName);
                        file.SaveAs(path);
                    }
                }
                catch (Exception ex)
                {
                    //ex something someting..
                }
            }
            return RedirectToAction("Index");  
        }

        return View(artikel);
    }

Edit:

    public ActionResult Edit(int id)
    {
        var model = new Files
        {
            Artikel = db.Artikler.Find(id),
            FileName = db.GetPictures(id, Server.MapPath("~/uploads/Artikler/"))

        };
        return View(model);
    }

HttpPost edit:

[HttpPost]
        public ActionResult Edit(Files files)
        {
            if (ModelState.IsValid)
            {
                db.Entry(files.Artikel).State = EntityState.Modified;
                db.SaveChanges();
                if (files.Artikel.FileUpload != null)
                {
                    foreach (var file in files.Artikel.FileUpload)
                    {
                        if (file.ContentLength > 0)
                        {
                            var folder = Server.MapPath("~/uploads/artikler/" + files.Artikel.Id.ToString());
                            if (!Directory.Exists(folder))
                            {
                                Directory.CreateDirectory(folder);
                            }

                        var fileName = Path.GetFileName(file.FileName);
                        var path = Path.Combine(folder, fileName);
                        file.SaveAs(path);
                    }
                }
            }
            return RedirectToAction("Index");
        }
        return View(files);
    }

The markup:

@FileUpload.GetHtml()

So, clearly ive misunderstood something, or done something wrong. Any ideas?

  • 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-25T13:16:03+00:00Added an answer on May 25, 2026 at 1:16 pm

    I guess the problem comes from the name attribute of the <input type="file"> element on the form.

    For example in the Create action it looks like this (I suppose because it works):

    <input type="file" name="FileUpload" />
    

    and then it is automatically bound to the FileUpload property of the Artikel class which is what your Create action takes as argument.

    In the Edit action I don’t know how it is named. But I suppose the same. But because your Edit action takes Files as argument it must be named like this:

    <input type="file" name="Artikel.FileUpload" />
    

    so that it is bound to the FileUpload property of the Artikel property of your Files model. Also you should make sure that the form containing this file input has the enctype="multipart/form-data" attribute.

    So fix your markup in the view.

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

Sidebar

Related Questions

I have a web application that needs to take a file upload from the
I have a User Control that contains a System.Web.UI.WebControls.FileUpload control as well as a
I have been working on a upload file that works fine on localhost (windows)
I have an ASP.NET web application that has a fileupload control to allow users
I have a page that have fileupload control, on the submission of the form,
Currently using System.Web.UI.WebControls.FileUpload wrapped in our own control. We have licenses for Telerik. I
I have a ListView that has a FileUpload control and a button in each
I am working on a web application which uses the FileUpload control. I have
I have one fileupload that can select multiple files. But the server side only
I have a FileUpload control. I am trying to save the file that is

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.