2013-10-09 140 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