HttpPostedFileBase每次都是空的

时间:2017-07-25 16:55:51

标签: c# file asp.net-core-mvc

我看到很多这样的问题,并在StackOverflow上搜索了所有其他案例,以回答为什么会出现这种情况而且没有一个应用。到目前为止,我能看到的一切都是正确的。我对文件输入标记的名称与控制器中create方法的变量名称完全相同。我甚至在表单中添加了enctype。见下文:

HTML:

@using (Html.BeginForm(new { enctype = "multipart/form-data" }))
{
    @Html.AntiForgeryToken()

    <div class="form-horizontal">
        <p><input type="file" name="file" id="file" /></p>
        <p><input type="submit" value="Update" class="btn btn-default" /></p>
    </div>
}

控制器:

[HttpPost]
    [ValidateAntiForgeryToken]
    public ActionResult Create(HttpPostedFileBase file) // This is where it's NULL
    {
        if (ModelState.IsValid)
        {
            IO io = new IO();
            if (file != null)
            {
                UpdateLog updateLog = io.updateIt(file);
                db.UpdateLogs.Add(updateLog);
                db.SaveChanges();
            } else
            {
                return RedirectToAction("Create");
            }


            return RedirectToAction("Index");
        }

        return View();
    }

1 个答案:

答案 0 :(得分:1)

我发现Html.BeginForm方法在cshtml中需要3个参数。我不得不手动指定方法和控制器。

@using (Html.BeginForm("Create", "UpdateLogs", FormMethod.Post, new { enctype = "multipart/form-data" }))
相关问题