c# - 为什么我的 HttpPostedFileBase 总是空的?

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

在我的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 文件 始终为空。另外 Request.Files.Count 始终为 0;

我的问题是什么。可以同时上传文件和接收 ETaskModel 数据吗?

附注uploadFile 文件上传的名称和 Controller 方法参数相同!

最佳答案

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

相反

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

关于c# - 为什么我的 HttpPostedFileBase 总是空的?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19271953/

相关文章:

c# - 从数字数组中找出所有的加减法组合

c# - 是否有关于使用 c# 进行 wifi 定位的好的框架/sdk?

c# - 使用 Razor 引擎避免 asp.net mvc 中的特殊字符

asp.net-mvc - 使用带有半相对URL的Url.Content

javascript - 获取RadioButton值Razor MVC

c# - 在 VS2010 中打开 .mdproj 文件

c# - 基于解决方案的条件汇编引用

asp.net-mvc - ASP.NET MVC(四)——按一定顺序绑定(bind)属性

asp.net-mvc - 对于 MVC 应用程序来说,web.config 中的 <customErrors> 去哪里了?

c# - 尝试在没有模型出现 "tree may not contain dynamic operation"错误的情况下使用 DropDownListFor