【问题标题】:Why is my HttpPostedFileBase always empty?为什么我的 HttpPostedFileBase 总是空的?
【发布时间】:2013-10-16 19:22:48
【问题描述】:

在我的Create View 我有

@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 数据吗?

附: uploadFile文件上传的名称与控制器方法参数一致!

【问题讨论】:

  • 这个问题似乎是题外话,因为它是关于一个错字。

标签: c# asp.net-mvc-4 razor file-upload asp.net-4.5


【解决方案1】:

我认为您在 BeginForm 上使用了错误的重载版本

相反

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

【讨论】:

    【解决方案2】:

    这是因为您的 Action 中的参数需要命名为 uploadFile 而不是 files 以匹配表单上提供的 id。然后,选择的文件将可用。

    【讨论】:

    • 我改变了它(只是忘了在这里正确写 - 在stackoverflow中)但它仍​​然不起作用
    【解决方案3】:

    尝试将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)
            {
              .
              .
            }
    

    没有检查代码,但这可能有效,希望这会有所帮助。

    【讨论】:

      猜你喜欢
      • 2016-12-07
      • 1970-01-01
      • 1970-01-01
      • 2013-03-15
      • 2020-05-07
      • 1970-01-01
      • 2020-07-20
      • 2020-07-11
      • 1970-01-01
      相关资源
      最近更新 更多