当我使用html helper而不是输入标记时,上传图像会返回null异常

时间:2017-01-29 16:03:30

标签: c# asp.net-mvc html-helper

大家好我正在尝试创建一个应用程序,允许用户将多个图像上传到博客帖子由于某种原因,当我使用它时,多个图像上传工作:

<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"  })
  

我得到的错误是一个空引用异常,但这些不一样吗?

它出现在这一行

 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");
}

1 个答案:

答案 0 :(得分:1)

因为这行代码

InfiniteLoop = True
Set ie = CreateObject("InternetExplorer.Application")
With ie
    .Visible = True
    .Navigate myURLLink ' should work for any URL

    SleepTime = 10000
    Do
        DoEvents
        Sleep (SleepTime)
        If ie.ReadyState >= 4 Then
            Exit Do
        End If
    Loop Until InfiniteLoop = False
End With

会生成像这样的HTML标记

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

请注意,input元素的名称仍然是“ImagePath”?因为HTML帮助器方法在构建元素名称属性值时使用属性名称。

如果要覆盖它,请使用大写属性名称。

这应该有效

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