首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >HttpPostedFileBase为空

HttpPostedFileBase为空
EN

Stack Overflow用户
提问于 2016-04-06 22:14:45
回答 1查看 61关注 0票数 0

我正在尝试上传一个文件,但它不能像预期的那样工作。我有以下的看法:

代码语言:javascript
复制
@using (Ajax.BeginForm("RegisterBand", "NewProfile", new AjaxOptions() { HttpMethod = "Post",
            InsertionMode = InsertionMode.Replace,
        }, new { enctype = "multipart/form-data"}))
        {
            @Html.AntiForgeryToken()
            @Html.ValidationSummary(true, "", new { @class = "text-danger" })


            <div class="form-horizontal">
                <div class="form-group">
                    <div class="col-md-10">
                        Bandname
                    </div>
                    <div class="col-md-10">
                        @Html.EditorFor(x => x.BandProfile.Name, new { htmlAttributes = new { @class = "form-control" } })
                        @Html.ValidationMessageFor(x => x.BandProfile.Name, "", new { @class = "text-danger" })
                    </div>
                </div>

                <div class="form-group">
                    <div class="col-md-10">
                        Genres
                    </div>
                    <div class="col-md-10">
                        @Html.DropDownListFor(x => x.BandProfile.Genres, Enumerable.Empty<SelectListItem>(), new { @class="", multiple = "multiple", style ="width: 100%;"} )
                        @Html.ValidationMessageFor(x => x.BandProfile.Genres, "", new { @class = "text-danger" })
                    </div>
                </div>

                <div class="form-group">
                    <div class="col-md-10">
                        Coverpicture
                    </div>

                    <div class="col-md-10">
                        <input type="file" name="file" id="CoverPicture" />

                        @Html.ValidationMessageFor(x => x.BandProfile.CoverPicture, "", new { @class = "text-danger" })
                    </div>
                </div>

                <div class="form-group">
                    <div class="col-md-10">
                        Description
                    </div>
                        <div class="col-md-10">
                            @Html.EditorFor(x => x.BandProfile.Description, new { htmlAttributes = new { @class = "form-control"} })
                            @Html.ValidationMessageFor(x => x.BandProfile.Description, "", new { @class = "text-danger" })
                        </div>
                    </div>

                <div class="form-group">
                    <div class="col-md-offset-2 col-md-10">
                        <input type="submit" value="Spara" class="btn btn-success" />
                    </div>
                </div>
            </div>
        }

这是我的控制器:

代码语言:javascript
复制
   [HttpPost]
        public ActionResult RegisterBand(ProfileViewModel model, HttpPostedFileBase file)
        {
            if (ModelState.IsValid == false)
            {
                return Json(JsonRequestBehavior.AllowGet);
            }

            var bandProfile = _profileService.CreateBandProfile(model.BandProfile, file, UserId);

            if (bandProfile != null)
            {
                userManager.AddToRole(UserId, "Band");
                return RedirectToAction("Index", "Welcome");
            }
            return View("Index");
        }

我遇到的问题是file总是导致null。我不明白为什么。如何找到问题所在?

EN

回答 1

Stack Overflow用户

发布于 2016-04-06 22:22:53

这里的问题是您正在使用Ajax.BeginForm()帮助器来创建和发布表单。但是,不能使用AJAX上传文件。

您可能想要考虑使用jQuery-based plug-in to accomplish this,它依赖于使用<iframe>在幕后处理您的上传操作,并将它们发布到适当的位置。

否则,您可以考虑使用Html.BeginForm()尝试一个普通表单,这在您的场景中应该可以工作(如果您不显式地需要任何AJAX功能)。

更新

这里的另一个问题是,用于Ajax.BeginForm()调用的构造函数接受AjaxOptionshtmlAttributes参数,这与此构造函数类似

但是,您当前的使用缺少第三个RouteValues参数。您可以尝试在其中添加null,看看是否有什么不同:

代码语言:javascript
复制
@using(Ajax.BeginForm("RegisterBand", 
                      "NewProfile", 
                      null, 
                      new AjaxOptions() { 
                             HttpMethod = "Post", 
                             InsertionMode = InsertionMode.Replace
                      }, 
                      new { enctype = "multipart/form-data"})){ 
     <!-- Content -->
}
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/36453792

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档