4

我知道有很多这样的问题,但我都阅读了它们,但其中没有一个解决了我的问题。下面我将展示我在视图和控制器中拥有的代码,以便您了解我的代码。

View :
<td><% using (Html.BeginForm("Index", "Home", FormMethod.Post, new {enctype = "multipart/form-data"})) { %>
<%: Html.ValidationSummary(true) %>
                <div id="section1"> 
    <div class="editor-label">
        <%: Html.LabelFor(model => model.Name) %>
    </div>
<label for="file">Filename:</label>
        <input type="file" name="myfile" id="File5" value="Choose File Banner1" />
     <p>&nbsp;</p>

And here is my controller:
[AcceptVerbs(HttpVerbs.Post)]
    public ActionResult Index(HomeModel model, string submitButton, HttpPostedFileBase myfile)
    {

我不知道是什么螨虫问题..有人可以帮我吗

4

6 回答 6

11

我也遇到了同样的问题,我在寻找答案时来到了这里。我忘记了表单定义中的 enctype 参数

@using (Html.BeginForm("Index", "Home", FormMethod.Post, new { enctype = "multipart/form-data" }))
{
// form
}
于 2013-12-21T14:31:42.997 回答
0

尝试在 enctype 附近的视图中使用 html 属性 "@data_ajax = "false" "

(Html.BeginForm("Index", "Home", FormMethod.Post, new {enctype = "multipart/form-data", @data_ajax = "false" }))
于 2014-09-26T15:17:51.270 回答
0

导致此问题的原因有很多:

  • 您没有在标签上指定enctype属性<form>
  • 你没有给你的标签一个name属性<input type="file">
  • 您将标签嵌套<form>在另一个标签中<form>

如果这些都不是您的问题,那么这对我有用:将 HttpPostedFileBase 属性添加到您的视图模型(或使用该属性创建视图模型)。

查看模型

public class MyViewModel
{
    public HttpPostedFileBase MyFile { get; set; }
}

控制器

public ActionResult Index()
{
    return View(new MyViewModel());
}

[HttpPost]
public ActionResult Index(MyViewModel viewModel)
{
    HttpPostedFileBase file = viewModel.MyFile;

    if (file != null && file.ContentLength > 0)
    {
        // Your code here
    }
}
于 2014-02-19T20:07:30.010 回答
0

可能是您value=""在输入元素中有一个集合吗?

于 2014-11-27T07:30:41.723 回答
0

我将一个 UI 复制到我正在工作的 MVC3 应用程序中,发现上传部分开始给出同样的错误。我发现页面布局中有一个额外的表单(使其成为嵌套表单)。我删除了它,一切对我来说都很好。

于 2015-03-28T07:42:55.530 回答
0

我之前遇到过同样的问题,我必须从文件输入中删除值才能使其工作:

<input type="file" name="myfile" id="File5" />
于 2013-10-31T11:59:01.943 回答