2017-07-25 120 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" }))