为什么文件上传后file's的值总是空的?

本文关键字:文件 file 为什么 | 更新日期: 2023-09-27 18:14:30

为什么在上传文件后,我使用调试器并始终获得文件的值为空。Type = file只使用一次。绑定正在工作。我使用了调试器,可以看到绑定前后文件的值,总是null

这是我的观点

@model WebApplication1.Models.Post
@{
    ViewBag.Title = "Create";
    Layout = "~/Views/Shared/_Layout.cshtml";
}
<h2>Add New Post</h2>

@using (Html.BeginForm()) 
{
    @Html.AntiForgeryToken()
    <div class="form-horizontal">
        <h4>Post</h4>
        <hr />
        @Html.ValidationSummary(true, "", new { @class = "text-danger" })
        <div class="form-group">
            @Html.LabelFor(model => model.PostTitle, htmlAttributes: new { @class = "control-label col-md-2" })
            <div class="col-md-10">
                @Html.EditorFor(model => model.PostTitle, new { htmlAttributes = new { @class = "form-control" } })
                @Html.ValidationMessageFor(model => model.PostTitle, "", new { @class = "text-danger" })
            </div>
        </div>
        <div class="form-group">
            @Html.LabelFor(model => model.PostAuthor, htmlAttributes: new { @class = "control-label col-md-2" })
            <div class="col-md-10">
                @Html.EditorFor(model => model.PostAuthor, new { htmlAttributes = new { @class = "form-control" } })
                @Html.ValidationMessageFor(model => model.PostAuthor, "", new { @class = "text-danger" })
            </div>
        </div>
        <div class="form-group">
            @Html.LabelFor(model => model.WebSite, htmlAttributes: new { @class = "control-label col-md-2" })
            <div class="col-md-10">
                @Html.EditorFor(model => model.WebSite, new { htmlAttributes = new { @class = "form-control" } })
                @Html.ValidationMessageFor(model => model.WebSite, "", new { @class = "text-danger" })
            </div>
        </div>
        <div class="form-group">
            @Html.LabelFor(model => model.PostDate, htmlAttributes: new { @class = "control-label col-md-2" })
            <div class="col-md-10">
                @Html.TextBoxFor(model => model.PostDate, new { @Value = @DateTime.Now.ToString("dd/MM/yyyy HH:mm:ss"), @readonly = "readonly" })
            </div>
        </div>
        <div class="form-group">
            @Html.LabelFor(model => model.PostText, htmlAttributes: new { @class = "control-label col-md-2" })
            <div class="col-md-10">
                @Html.EditorFor(model => model.PostText, new { htmlAttributes = new { @class = "form-control" } })
                @Html.ValidationMessageFor(model => model.PostText, "", new { @class = "text-danger" })
            </div>
        </div>

        @{
         ViewBag.Title = "File Upload in MVC3 By Using Razor";
        }
        @using (Html.BeginForm("Create", "BlogController", FormMethod.Post , new { enctype = "multipart/form-data"}))
        {
            <div>
                <b><u>File Upload in MVC3 By Using Razor</u></b>
                Select Image
                <input type="file" name="file" />
                <input type="submit" value="Upload Image" name="Command" /><br />
            </div>
            <div>
                @ViewBag.Message
            </div>
        }

        <div class="form-group">
            <div class="col-md-offset-2 col-md-10">
                <input type="submit" value="Create" class="btn btn-default" />
            </div>
        </div>
    </div>
}
<div>
    @Html.ActionLink("Back to Posts List", "Index")
</div>
@section Scripts {
    @Scripts.Render("~/bundles/jqueryval")
}

下面是我在BlogController

中的创建操作
 public ActionResult Create([Bind(Include = "PostID,PostTitle,PostAuthor,WebSite,PostDate,PostText,PostImage,PostVideo")] Post post, HttpPostedFileBase file)
        {
            if (ModelState.IsValid)
            {
                if (file != null)
                {
                    file.SaveAs(HttpContext.Server.MapPath("~/Images/") + file.FileName);
                    post.PostImage = file.FileName;
                    return RedirectToAction("Index");
                }
                db.Posts.Add(post);
                db.SaveChanges();
                return RedirectToAction("Index");
            }
            return RedirectToAction("Index");
        }

为什么文件上传后file's的值总是空的?

不要使用嵌套表单,单独创建一个动作"uploadFile"并检查文件绑定"HttpPostedFile Base file"相同的名称,就像在表单