Sunday, December 6, 2026Software, technology and IT culture since 2014
Coding

ASP.NET MVC - How to handle one or more uploaded files as ViewModel properties using HttpPostedFileBase class

How to handle one or more POST multipart uploaded files as ViewModel properties using HttpPostedFileBase in ASP.NET MVC 4, MVC5, MVC6 and ASP.NET Core MVC.

Dealing with file uploads in ASP.NET / ASP.NET Core MVC can be tricky, expecially if we want to stick to the ViewModel pattern and handle them just like standard properties in our POCO classes.

If it weren't the case, we could easily work around it and do something like this in the View:

C#
@model SomeModel;
 
<!-- some HTML code -->
 
@using (Html.BeginForm("Submit", "Home", FormMethod.Post, new { enctype="multipart/form-data" }))
{
    <!-- some @Html.TextBoxFor() and other razor form controls bound to the model -->
 
    <input type="file" name="someFile" />
 
    <input type="submit" value="Upload" name="UploadButton" id="UploadButton" />
}

And then retrieve the file in the Controller in the following way:

C#
[HttpPost]
public ActionResult Submit(SomeModel model, HttpPostedFileBase someFile)
{
   // do something with someFile
 
   return View(model);
}

To put it short, the file is kept outside the model and treated in a separate way.

This is a common and practical way to handle file uploads: I stumble upon this a number of times, from friends code to some GitHub project. Although being perfectly fine, it's definitely not the proper way to deal with such scenario: what if we are supposed to receive multiple files? Wouldn't be much better to have them inside the ViewModel instead?

Luckily enough, the MVC framework allows us to deal with it in a much better and cleaner way. Here's how:

In the ViewModel:
*/ ?>
C#
public class SomeModel() 
{ 
    public SomeModel() 
    {
    }
 
    public HttpPostedFileBase SomeFile{ get; set; }
}
In The View:
C#
@model SomeModel
 
<!-- some stuff -->
 
@using (Html.BeginForm("Submit", "Home", FormMethod.Post, new { enctype="multipart/form-data" }))
{
    <!-- some @Html.TextBoxFor() and other razor form controls bound to the model -->
 
    @Html.TextBoxFor(m => m.SomeFile, new { type = "file" })
 
    <input type="submit" value="Upload" name="UploadButton" id="UploadButton" />
}
In the Controller:
C#
[HttpPost]
public ActionResult Submit(SomeModel model)
{
    // do something with model.SomeFile
 
    return View();
}

That's it!

In case we need to support multiple files, we are free to choose between the following:

  • create multiple properties and implement them separately just like the one above;
  • change the public HttpPostedFileBase SomeFile  property to something like public List<HttpPostedFileBase> SomeFiles  and then span multiple @Html.TextBoxFor(m => m.SomeFile, new { type = "file" })  controls.

As always, the choice is ours.

*/ ?>
Share
Author

Ryan

IT Project Manager, Web Interface Architect and Lead Developer for many high-traffic web sites & services hosted in Italy and Europe. Since 2010 it's also a lead designer for many App and games for Android, iOS and Windows Phone mobile devices for a number of italian companies. Microsoft MVP for Development Technologies since 2018.

All articles by Ryan
Discussion

Comments

Thanks a lot man, you solved my issue

Reply

mate you just made an unproductive day in a productive day thanks

Reply

Glad to hear that! If you don’t mind, like us on FB/TW if you found our post useful :)

Reply

Thanks so much, I have tried this in Ajax.BeginForm but it doesn’t work, the same code in Html.BiginForm works well, does ajax not allow to upload file or should I adjust my code somewhere? And sorry for my bad English if anything not clear just tell me, thx!

Reply

Hello there,
I’m happy you did like our post: if you found it useful, don’t forget to add a like on our Facebook page :)

Regarding your query, I personally do not like Ajax.BeginForm for a number of reasons: whenever I need to post a form using AJAX, I always end up using a JQuery form plugin or other JS-based tools that allow me to manually setup and fine-tune the ajax request/response cycle.

Here’s a good example from StackOverflow, including full source code:

https://stackoverflow.com/a/19044689/1233379

Reply

Leave a comment

Your email address will not be published.


The reCAPTCHA verification period has expired. Please reload the page.

This site uses Akismet to reduce spam. Learn how your comment data is processed.

*/ ?>

Newsletter

Get our new articles, in-depth pieces and updates on our software projects.

We will ask you to confirm your subscription by email. You can unsubscribe at any time; your data is handled as described in the privacy policy.

{"prefetch":[{"source":"document","where":{"and":[{"href_matches":"/*"},{"not":{"href_matches":["/wp-*.php","/wp-admin/*","/wp-content/uploads/*","/wp-content/*","/wp-content/plugins/*","/wp-content/themes/monstrance-ryadel/*","/wp-content/themes/monstrance/*","/*\\?(.+)","/*ao_noptirocket*","/*jetpack=comms*","/*kinsta-monitor*","/*ao_speedup_cachebuster*","/*removed_item*","/my-account*","/wc-api/*","/edd-api/*","/wp-json*"]}},{"not":{"selector_matches":"a[rel~=\"nofollow\"]"}},{"not":{"selector_matches":".no-prefetch, .no-prefetch a"}}]},"eagerness":"conservative"}]}