簡體   English   中英

HttpPostedFileBase始終返回null ASP.NET MVC 5

[英]HttpPostedFileBase always return null ASP.NET MVC 5

我嘗試在ASP.NET MVC5中上傳文件時遇到問題。

不知何故,“ HttpPostedFileBase文件”始終返回null,我找不到我的問題。

我的代碼:(控制器)

 public ActionResult Edit(PMNieuwePloeg ploeg, FormCollection frm, HttpPostedFileBase file)
    {
        if (file != null && file.ContentLength > 0)
        {
            var fileName = Path.GetFileName(file.FileName);
            var path = Path.Combine(Server.MapPath("/Content/ImagesPloegen/"), fileName);
            file.SaveAs(path);

            ploeg.NieuwPloeg.ImageUrl = file.FileName;
        }
        else
        {
            ploeg.NieuwPloeg.ImageUrl = "/geen.jpg";
        }

        if(ploeg.NieuwPloeg.LandID > 0)
        {
            ploeg.NieuwPloeg.UserId = UserManager.FindByName(User.Identity.Name).Id;
            ploeg.NieuwPloeg.UserName = UserManager.FindByName(User.Identity.Name).UserName;
            repoPloegen.Edit(ploeg.NieuwPloeg);

        }
        return RedirectToAction("Index");

    }

視圖

@using (Html.BeginForm("Edit","Ploegen", FormMethod.Post, new { enctype = "multipart/form-data" }))
{
@Html.AntiForgeryToken()
<div class="form-horizontal">
    <h4>Ploeg</h4>


    <div class="form-group">
        @Html.LabelFor(model => model.NieuwPloeg.ImageUrl, "Ploeg Image", htmlAttributes: new { @class = "control-label col-md-2" })
        <div class="col-md-10">
            <input type="file" name="file" required />

        </div>
    </div>


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

}

有人可以在這里發現問題嗎?

在控制器中

 HttpPostedFileBase casteCertificate = Request.Files["UploadRegCard"];

鑒於

@using (Html.BeginForm("ActionMethod", "Controller", FormMethod.Post, new { enctype = "multipart/form-data", name = "myForm", id = "myForm" }))
    {
     <input type="file" name="UploadRegCard" id="UploadRegCard" class="form-control" />
}

使用輸入元素的名稱屬性,您可以從視圖到控制器檢索文件

檢查控制器中的Request.Files數組。 它應包含您從客戶端發布的所有文件

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM