我看到了许多类似的问题,并在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();
}发布于 2017-07-26 01:17:16
我发现Html.BeginForm方法需要cshtml中的3个参数。我必须手动指定方法和控制器。
@using (Html.BeginForm("Create", "UpdateLogs", FormMethod.Post, new { enctype = "multipart/form-data" }))https://stackoverflow.com/questions/45309458
复制相似问题