asp.net mvc的文件上传为空查看模型传递 [英] asp.net mvc file upload is passed as null to view model

查看:92
本文介绍了asp.net mvc的文件上传为空查看模型传递的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个上传照片到我的数据库表格,我用一个视图模型在这个过程中提供帮助。

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

查看产品型号:

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

当我调试code,我看到在我的控制器post方法,一切从表格中的信息,除了在uploadFile是空视图模型更新。
在表单我使用ENCTYPE =多部分/表单数据。当我用我的母版页的uploadFile为空,但是当我使用默认的MVC母版页一切工作正常。

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.

下面是我的母版页:

<%@ 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的母版页

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>


任何想法?

推荐答案

您还没有公布与被贴在表格的实际看,所以我只能猜测:

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

请确保你已经确立了自己的形式为multipart / form-data的的加密类型,否则文件将不会被上传。

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>

或与HTML助手:

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

这篇关于asp.net mvc的文件上传为空查看模型传递的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆