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

寵の児 提交于 2019-12-23 14:23:59

问题


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?


回答1:


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" })) {
...
<% } %>



回答2:


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.




回答3:


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

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


来源:https://stackoverflow.com/questions/3526691/asp-net-mvc-file-upload-is-passed-as-null-to-view-model

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!