asp.net-mvc – asp.net mvc文件上传作为null传递给视图模型

我有一个将照片上传到我的数据库的表单,我使用视图模型来帮助完成此过程.

查看型号:

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;
    }
}

当我调试代码时,我看到在我的控制器的post方法中,表单中的所有信息都在视图模型中更新,但uploadFile为null.
在表单中我使用enctype =“multipart / form-data”.当我使用我的母版页时,uploadFile为null但是当我使用默认的MVC母版页时,一切正常.

这是我的母版页:

<%@ 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>     

这是默认的asp.net mvc母版页

<%@ 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>

有任何想法吗?

最佳答案 您尚未使用发布的表单发布实际视图,因此我只能猜测:

确保您已将表单的enctype设置为“multipart / form-data”,否则将无法上载文件.

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

或者使用Html Helper:

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