1925

asp.net mvc file upload is passed as null to view model

Question:

I have a form which uploads a photo to my database, and I use a view model to aid in this process.

View Model:

public class GalleryViewModel { //Members: public Gallery _photo { get; set; } public string _title { get; set; } public string _description { get; set; } public string _photographer { get; set; } public HttpPostedFileBase uploadFile { get; set; } // Ctor public GalleryViewModel(Gallery photo) { _photo = photo; } public GalleryViewModel() { _photo = null; } }

When I debug the code, I see that the in the post method in my controller, all the information from the form is updated in the view model except for the uploadFile which is null. In the form I use enctype = "multipart/form-data". When I use my master page the uploadFile is null but when I use the default MVC master page everything works fine.

Here is my master page:

<%@ Master Language="C#" MasterPageFile="~/views/Shared/GeneralMaster.master" Inherits="System.Web.Mvc.ViewMasterPage" %>

<asp:Panel ID="notifiactions" runat="server"> <% if (ViewData["notifications"] != null) { %> <br /> <table width="100%"> <tr> <td align="center"> <div id="messages" style="width: 90%; border: solid 1px #A3A3A3"> <br /> <%= Html.Encode(ViewData["notifications"])%> <br /><br /> </div> </td> </tr> </table> <% } %> </asp:Panel> <br /> <table width="100%" cellpadding="0" cellspacing="0"> <tr> <td width="30%" align="center"> <img src="\Content\SiteDesign\wine_in_frame2.JPG" alt="lk" /> </td> <td width="40%"> <asp:ContentPlaceHolder ID="PageContent" runat="server" /> </td> <td width="30%" align="right"> <img src="\Content\SiteDesign\set_with_frame.jpg" alt="Alon" /> </td> </tr> </table>

Here is the default asp.net mvc master page

<%@ Master Language="C#" Inherits="System.Web.Mvc.ViewMasterPage" %>

<div id="header"> <div id="title">

My MVC Application

</div> <div id="logindisplay"> <% Html.RenderPartial("LogOnUserControl"); %> </div> <div id="menucontainer"> <ul id="menu"> <li><%= Html.ActionLink("Home", "Index", "Home")%></li> <li><%= Html.ActionLink("About", "About", "Home")%></li> </ul> </div> </div> <div id="main"> <asp:ContentPlaceHolder ID="MainContent" runat="server" /> <div id="footer"> </div> </div> </div>

Any Ideas?

Answer1:

You have not posted the actual view with the form that is posted, so I can only guess:

Make sure, you have set the enctype of your form to "multipart/form-data", otherwise files won't be uploaded.

<form id="form" name="form" action"controller/action" enctype="multipart/form-data"> ... </form>

or with an Html Helper:

<% using(Html.BeginForm("action", "controller", "POST", new { enctype = "multipart/form-data" })) { ... <% } %>

Answer2:

Turns out the the master page I was using inherits from my project's main master page.

In this master page there was a <form></form> tag encapsulating the <asp:contentholder>. removing the form tags fixed this problem.

Answer3:

adding this code to form will get multi part data on the form eg: attachement etc.

new { enctype = "multipart/form-data" }

Recommend

  • Flutter: Google Play services out of date
  • how to find record insert to mysql using commit()
  • Stoping jquery .then chain via user input
  • Fastest way to download multiple urls
  • Why does Maven disable caching for HTTP requests?
  • [program name].exe has stopped working
  • Kotlin - Throw Custom Exception
  • Matlab: How to find a point in a bounding box
  • What's wrong with my PNG IDAT chunk?
  • convert eye-tracking .edf file to ASC/CSV format
  • How do non-HTML5 JavaScript HSV color pickers work?
  • Encode string to Base64 in Inno Setup (Unicode Version of Inno Setup)
  • Parallelization via JDBC - Pyspark - How does parallelization work using JDBC?
  • Paging Through XML Data Using jQuery and HTML
  • Passing HTTP Post from AWS API GW to Lambda
  • Is there a .NET method equivalent to NETWORKDAYS in Excel?
  • Combine two jagged lists into one
  • Corda: How to implement hierarchical relationships between state data persisted to H2
  • How to search for a method or variable name within android project?
  • Disabling swipe gesture in Windows Phone 8.1 pivot control
  • Stop an element moving with padding on hover
  • Django, uWSGI & nginx: Process dies for “no reason”
  • using maven pom while creating jar:test-jar some times it says JAR will be empty - no content was ma
  • concise way of flattening multiindex columns
  • Neo4j…how to get a visual representation of my data?
  • Python 3x- Compression Makes File Bigger :(
  • Can a PHP script be scheduled to run at a specific time or after a specific amount of time has expir
  • How to integrate angular2-material (alpha 8.2) with angular2-Quickstart app
  • Bad automatic Triangulation with Mayavi for coloring a surface known only by its corner
  • Running R's aov() mixed effects model from Python using rpy2