2013-10-09 211 views
2

在我CreateView我有爲什麼我的HttpPostedFileBase總是空的?

@using (Html.BeginForm(new { ReturnUrl = ViewBag.ReturnUrl, FormMethod.Post, enctype = "multipart/form-data" })) 
     { 
      @Html.AntiForgeryToken() 
      @Html.ValidationSummary(true) 

     <div> 
      <h2>New Task</h2> 
      <ol style="list-style-type: none;"> 
       <li> 
        @Html.LabelFor(m => m.Title, htmlAttributes: new { @class = "formlabel" }) 
        @Html.TextBoxFor(m => m.Title) 
        @Html.ValidationMessageFor(m => m.Title) 
       </li> 
       <li> 
        @Html.LabelFor(m => m.Description, htmlAttributes: new { @class = "formlabel" }) 
        @Html.TextAreaFor(m => m.Description) 
        @Html.ValidationMessageFor(m => m.Description) 
       </li> 
       <li> 
        @Html.LabelFor(m => m.Deadline, htmlAttributes: new { @class = "formlabel" }) 
        @Html.TextBoxFor(m => m.Deadline, htmlAttributes: new { id = "date-picker", type = "text", @class = "hasDatepicker" }) 
        @Html.ValidationMessageFor(m => m.Deadline) 
       </li> 
       <li> 
        @Html.LabelFor(m => m.RankID, htmlAttributes: new { @class = "formlabel" }) 
        @Html.DropDownList("RankID", null, htmlAttributes: new { @class = "standselect" }) 
        @Html.ValidationMessageFor(m => m.RankID) 
       </li> 
       <li> 
        @Html.LabelFor(m => m.PriorityID, htmlAttributes: new { @class = "formlabel" }) 
        @Html.DropDownList("PriorityID", null, htmlAttributes: new { @class = "standselect" }) 
        @Html.ValidationMessageFor(m => m.PriorityID) 
       </li> 
       <li> 
        <label for="uploadFile">Files</label> 
        <input type="file" name="uploadFile" id="uploadFile" /> 
       </li> 
       <li style="margin: 20px 0 0 32px;"> 
        <input type="submit" class="ghButton btn btn-navy" value="Create" /> 
       </li> 
      </ol> 
     </div> 
    } 

在我Controller我有

 [HttpPost] 
     public ActionResult Create(ETaskModel taskModel, HttpPostedFileBase uploadFile) 
     { 
      var tasksServ = new TasksService(); 

      //var files = Request.Files;//files 
      var upFiles = uploadFile;//up files 

      //returning recently created task 
      DataAccess.Task createdTask; 
      tasksServ.Create(taskModel.Title, taskModel.RankID, SessionHelper.User.ID, taskModel.Deadline, taskModel.Description, taskModel.PriorityID, 
       null, //---------documents 
       null, //implementator users 
       out createdTask); 


      var generalServ = new General(); 
      ViewBag.RankID = new SelectList(generalServ.GetRanks(), "RankID", "RankValue", taskModel.RankID); 
      ViewBag.PriorityID = new SelectList(generalServ.GetPriorities(), "PriorityID", "Name", taskModel.PriorityID); 
      return View(taskModel); 
     } 

在提交時我收到我的ETaskModel taskModel對象數據。但HttpPostedFileBase files始終爲空。另外Request.Files.Count總是0;

我的問題是什麼。有可能上傳文件並同時接收ETaskModel數據?

P.S. uploadFile文件上傳的名稱和控制器的方法參數是一樣的!

+0

這個問題似乎是題外話,因爲它是關於一個錯字。 – Stijn

回答

2

我認爲你正在使用BeginForm

錯誤重載版本而不是

Html.BeginForm(null, null, FormMethod.Post, new { ReturnUrl = ViewBag.ReturnUrl, enctype = "multipart/form-data" }) 
1

這是因爲您的操作中的參數需要命名爲uploadFile而不是files以匹配表單上提供的id。然後,所選的文件將可用。

+0

我改變了它(只是忘了在這裏寫入 - 在stackoverflow)但它仍然不工作 – levi

+0

邁克爾你是一個救命! – cbillowes

1

嘗試添加HttpPostedFileBase到模型中,

public class ETaskModel 
    { 
     public string Title {get; set;} 
     public string Description{get; set;} 
     . 
     . 
     public HttpPostedFileBase uploadFile {get; set;} 
    } 

,並在你的控制器,

 [HttpPost] 
     public ActionResult Create(ETaskModel taskModel) 
     { 
      . 
      . 
     } 

Didnt檢查代碼,但是這可能會奏效,希望這有助於。

+0

nope。沒有工作 – levi