为什么我的HttpPostedFileBase总是为空?

列维

在我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数据?

PS uploadfile文件上传的文件名和控制器方法参数相同!

MRB

我认为您使用的是错误的重载版本 BeginForm

代替

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

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

为什么MVC HttpPostedFileBase总是为null?

来自分类Dev

WebApi - 为什么我的帖子变量总是为空?

来自分类Dev

函数参数总是为空为什么?

来自分类Dev

为什么 PropertyChanged 总是为空?

来自分类Dev

为什么我的KendoGrid“更新”参数在控制器中总是为空?

来自分类Dev

为什么我的req.body在POST上总是为空?

来自分类Dev

为什么数组在响应中总是为空?

来自分类Dev

Elasticsearch,为什么添加元素总是为空

来自分类Dev

Asp .Net MVC,为什么我的viewbag 总是不为空?

来自分类Dev

为什么我的请求总是带有空值

来自分类Dev

为什么我的jQuery空为无效?

来自分类Dev

为什么我的向量为空?

来自分类Dev

为什么我的JavaFX TableView为空

来自分类Dev

为什么我的InitializeComponent子为空?

来自分类Dev

为什么我的MultipartFile为空

来自分类Dev

为什么我的屏幕比例为空?

来自分类Dev

为什么我的对象保持为空?

来自分类Dev

为什么我的WPF CompboBox为空

来自分类Dev

为什么我的变量为空?

来自分类Dev

为什么我的请求正文为空?

来自分类Dev

为什么我的清单为空?

来自分类Dev

AVCapturePhoto 为什么我的照片总是为零?

来自分类Dev

为什么我的数组列表的大小总是为 1?

来自分类Dev

HttpPostedFileBase始终为空

来自分类Dev

为什么用户的活动总是空的?

来自分类Dev

为什么“datas”数组总是空的?

来自分类Dev

为什么post参数总是来自Reactjs的asp.net核心Web API为空?

来自分类Dev

我不知道为什么我的变量总是重置为0?

来自分类Dev

为什么即使立即实例化我的字段也为空?