为什么文件上传后文件的值始终为null

时间:2015-09-24 20:00:25

标签: c# asp.net-mvc

为什么在上传文件后,我使用调试器并始终将文件的值设为null。 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");

        }

1 个答案:

答案 0 :(得分:0)

不要使用嵌套表单,制作一个单独的动作“uploadFile”并检查文件绑定“HttpPostedFile Base file”的相同名称,如表格