English 中文(简体)
页: 1
原标题:asp.net mvc file upload is passed as null to 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.

页: 1

<%@ 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="ContentSiteDesignwine_in_frame2.JPG" alt="lk" />
            </td>
            <td width="40%">
                <asp:ContentPlaceHolder ID="PageContent" runat="server" />
            </td>  
            <td width="30%" align="right"> 
                <img src="ContentSiteDesignset_with_frame.jpg" alt="Alon" />
            </td>
        </tr>
    </table>     

这里是缺省总页。

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

任何想法?

问题回答

你没有以所张贴的形式发表实际看法,因此我只能猜测:

确实,你把你的形式打上了“多部分/格式数据”的一类,否则,卷宗就用上了。

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

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

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

引出主页,是利用我项目主页的继承。

在主页上有一个<代码><form></form> tag encapsulating the <asp:content holder>。 消除形式标签将这一问题固定下来。





相关问题
WebForms and ASP.NET MVC co-existence

I am trying to make a WebForms project and ASP.NET MVC per this question. One of the things I ve done to make that happen is that I added a namespaces node to the WebForms web.config: <pages ...

Post back complex object from client side

I m using ASP.NET MVC and Entity Framework. I m going to pass a complex entity to the client side and allow the user to modify it, and post it back to the controller. But I don t know how to do that ...

Create an incremental placeholder in NHaml

What I want to reach is a way to add a script and style placeholder in my master. They will include my initial site.css and jquery.js files. Each haml page or partial can then add their own required ...

asp.net mvc automapper parsing

let s say we have something like this public class Person { public string Name {get; set;} public Country Country {get; set;} } public class PersonViewModel { public Person Person {get; ...

structureMap mocks stub help

I have an BLL that does validation on user input then inserts a parent(PorEO) and then inserts children(PorBoxEO). So there are two calls to the same InsertJCDC. One like this=>InsertJCDC(fakePor)...

ASP.NET MVC: How should it work with subversion?

So, I have an asp.net mvc app that is being worked on by multiple developers in differing capacities. This is our first time working on a mvc app and my first time working with .NET. Our app does not ...

System.Web.Mvc.Controller Initialize

i have the following base controller... public class BaseController : Controller { protected override void Initialize(System.Web.Routing.RequestContext requestContext) { if (...

热门标签