2017-07-25 62 views
0

我看到很多像这样的问题,并通过StackOverflow上的所有其他案例进行搜索,找出答案,为什么这是这种情况,并没有应用它们。我能看到的所有东西都是正确的。我的文件输入标记名称与控制器中create方法的变量名称完全相同。我甚至在表单中添加了enctype。请看下图:HttpPostedFileBase每次都是空的

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

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

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