【问题标题】:No file chosen even when file is chosen - mvc即使选择了文件,也没有选择文件 - mvc
【发布时间】:2015-11-07 06:21:00
【问题描述】:

我正在更新记录,因此我正在查看我创建的编辑页面,当我更改值并选择要上传的另一个文件而不是以前的文件并按下更新按钮时,我看到的错误是“未选择文件”在选择文件控件下方。为什么我在选择文件的时候会这样。

我正在使用以下代码进行文件上传:

   <div class="form-group">
        @Html.LabelFor(model => model.File, new { @class = "control-label col-md-2" })
        <div class="col-md-10">
            @Html.TextBoxFor(model => model.File, new { type = "file", accept = "image/*" })
            @Html.ValidationMessageFor(model => model.File)
        </div>
    </div>

在创建此记录时,使用了相同的代码并且工作正常,那么尝试更新记录时会出现什么问题?

【问题讨论】:

  • 模型的File属性的数据类型是什么?

标签: .net razor model-view-controller asp.net-mvc-5


【解决方案1】:

我找到了解决办法。

如果您遇到同样的问题,唯一要做的就是检查您的 .cshtml 文件是否有此行,如果没有将您的 @html.beginForm 替换为类似以下内容:

@using (Html.BeginForm("Edit", "Stations", FormMethod.Post, new { enctype = "multipart/form-data" }))

【讨论】:

    【解决方案2】:

    您已将 File 属性设为必需。所以你正面临这个问题。 从文件中删除 [必需] 属性。

    [必填]
    公共 IEnumerable 文件 { 获取;放; }

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-10-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多