Asp.net mvc 2 ASP.NET MVC2+;文件上载(HttpPostedFileBase类)

Asp.net mvc 2 ASP.NET MVC2+;文件上载(HttpPostedFileBase类),asp.net-mvc-2,file-upload,httppostedfilebase,Asp.net Mvc 2,File Upload,Httppostedfilebase,我上传文件有问题。我想从我的编辑视图上传它: <% using (Html.BeginForm("edit","profile",FormMethod.Post, new { enctype="multipart/form-data" })) {%> <%: Html.ValidationSummary(true) %> <%: ViewData["ErrorMessage"] %> <fieldset

我上传文件有问题。我想从我的编辑视图上传它:

    <% 
    using (Html.BeginForm("edit","profile",FormMethod.Post, new { enctype="multipart/form-data" }))
    {%>
    <%: Html.ValidationSummary(true) %>

    <%: ViewData["ErrorMessage"] %>
    <fieldset>
        <legend>Fields</legend>

        <div class="editor-label">
            <%: Html.LabelFor(model => model.Image) %>
        </div>
        <div class="editor-field">
            <input type="file" id="Image" name="Image" />
            <label id="LabelErrorImage" class="errorMessage" />
        </div> 

        <p>
            <input type="submit" value="Save" onclick="return Validate(); return false;"/>
        </p>
    </fieldset>

<% } %>
但我总是有空的文件对象,为什么?????你有什么想法,建议,为什么它可以这样工作?我如何将文件值传递给编辑操作可能有问题


编辑: 这真的很奇怪,甚至当我删除
使用(Html.BeginForm(“Index”,“Profile”,FormMethod.Get,new{enctype=“multipart/formdata”}))

页面源仍然具有:

<body>

    <form method="post" action="6111e591-b92d-4bcb-b214-ab8f664b35f9" id="form1">

我的意思是我无法更改标签,但不知道为什么://

尝试更改:-

public ActionResult Edit(string id, HttpPostedFileBase file, 
  FormCollection formValues)
致:-

因为您输入的名称是
image

 <input type="file" id="Image" name="Image" />

编辑 老实说,还有一件事阻止了图像的绑定。这是你贴的全部表格吗

一些需要测试的东西

  • 您有两次HTTPOST装饰您的方法,尽管我不认为这会有什么不同
  • 查看源文件,确保源文件中没有其他名为
    name=image
    的文件
  • 再次测试之前,请确保清空缓存并确保源代码正确
  • 尝试使用
  • 从上次编辑判断,母版页/布局有问题吗?这是mvc/webforms的混合版本吗

  • 在您的视图中,您将文件输入命名为“image”,但您的操作方法接受一个名为“file”的参数。重命名其中一个,它应该可以工作。

    此问题的解决方案如下:

  • 我们使用Master.Site
  • 我们想在视图中上传文件
  • 我们确信它会起作用但我们一直都是空的
  • 然后:

  • 男人们是对的-我的名字错了-检查一下
  • 检查视图的源代码,如果您有2个
    标记,您应该从主站点删除标记,因为第二个标记将被忽略
    现在它应该可以工作了。

    是的,是的,我已经更改了它,但它仍然为空:/n它不是所有的代码,因为它很长,但我注意到,在我的视图中写什么并不重要,因为它总是重定向到同一个函数。可能是因为我使用了主站点,它使用了一种我不知道的机制?而且我在源代码中有两种形式:然后是我在视图中写的第二种形式:是的,我发现了错误!!!!在Master.Site中,它默认创建表单,不能嵌套表单,因为第二个表单被忽略!我已经浪费了很多时间去发现它!谢谢你的帮助!不用担心,也许你可以把这个问题标记为已回答,谢谢,但我现在已经改变了,仍然是一样的:/
    public ActionResult Edit(string id, HttpPostedFileBase image, 
      FormCollection formValues)
    
     <input type="file" id="Image" name="Image" />