当我使用 html 帮助程序而不是输入标签时,上传图像 returns 出现空异常

Uploading an image returns a null exception when I used html helper instead of an input tag

大家好我正在尝试创建一个允许用户将多张图片上传到博客的应用程序post出于某种原因当我使用这个时多张图片上传工作:

<input id="ImagePath" title="Upload a product image" multiple="multiple" type="file" name="files" />

但当我像这样使用 Html 助手时就不会了

 @Html.TextBoxFor(model => model.ImagePath, new { type = "file", multiple = "multiple", name = "files"  })

The error I get is a null reference exception but are these not the same?

出现在这一行

 foreach (var file in files)

查看

@model Crud.Models.PostModel
@{
    ViewBag.Title = "Create";
}
<h2>Create</h2>
@using (Html.BeginForm("Create", "Home", null, FormMethod.Post, new { enctype = "multipart/form-data" }))
{
    @Html.AntiForgeryToken()
    @Html.ValidationSummary(true)
    <fieldset>
        <div class="editor-label">
            @*@Html.LabelFor(model => model.ImagePath)
            @Html.TextBoxFor(model => model.ImagePath, new { type = "file", multiple = "multiple", name = "files"  })
            @Html.ValidationMessageFor(model => model.ImagePath)*@


            @Html.LabelFor(model => model.ImagePath)
            <input id="ImagePath" title="Upload a product image" multiple="multiple" type="file" name="files" />
        </div>

        <div class="editor-field">
            @Html.LabelFor(model => model.Heading)
            @Html.TextBoxFor(model => model.Heading)
            @Html.ValidationMessageFor(model => model.Heading)
        </div>
        <div>
            @Html.LabelFor(model => model.PostBody)
            @Html.TextBoxFor(model => model.PostBody)
            @Html.ValidationMessageFor(model => model.PostBody)
        </div>
        <p><input type="submit" value="Create" /></p>
    </fieldset>
}
<div>
    @Html.ActionLink("Back to List", "Index")
</div>
@section Scripts {
    @Scripts.Render("~/bundles/jqueryval")
}

控制器

public ViewResult Create()
{
    return View("Create", new PostModel());

}

[HttpPost]
public ActionResult Create(PostModel Post, IEnumerable<HttpPostedFileBase> files)
{
    if (ModelState.IsValid)
    {
        foreach (var file in files)
        {
            PostModel post = new PostModel();
            if (file.ContentLength > 0)
            {
                string displayName = file.FileName;
                string fileExtension = Path.GetExtension(displayName);
                string fileName = string.Format("{0}.{1}", Guid.NewGuid(), fileExtension);
                string path = Path.Combine(Server.MapPath("~/Img/"), fileName);
                file.SaveAs(path);
                post.ImageDisplayName = displayName;
                post.ImagePath = fileName;
                post.PostBody = Post.PostBody;
                post.Heading = Post.Heading;
            }
            repository.Save(post);

        }
    }
    return RedirectToAction("display");
}

因为这行代码

@Html.TextBoxFor(model => model.ImagePath, n
                            ew { type = "file", multiple = "multiple", name = "files"  })

将生成这样的 html 标记

<input id="ImagePath" multiple="multiple" name="ImagePath" type="file" value="">

注意到输入元素的名称仍然是 "ImagePath" 吗?因为 HTML 辅助方法在构建元素名称属性值时使用了 属性 名称。

如果要覆盖它,请使用 UPPER CASE 属性名称。

这应该有效

@Html.TextBoxFor(model => model.ImagePath, 
                           new { type = "file", multiple = "multiple", NAME = "files"  })