views:

63

answers:

2

Hi,

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">
            <h1>My MVC Application</h1>
        </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?

A: 

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" })) {
...
<% } %>
Dave
I'm using:<% using (Html.BeginForm("UploadPhoto", "Gallery", FormMethod.Post, new { enctype = "multipart/form-data" })) {%>As I said, when I debug the code, pushing the upload button does call the post method, and all the information in the view model is updated when I call UpdateModel, except for the file which is null...
Eran
A: 

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.

Eran