C# 始终错误对象引用未设置为HttpPostedFileBase集合中对象的实例

C# 始终错误对象引用未设置为HttpPostedFileBase集合中对象的实例,c#,asp.net-mvc,nullreferenceexception,C#,Asp.net Mvc,Nullreferenceexception,为什么在使用IEnumerable filebase时出错 我使用这个代码 @using (Html.BeginForm("Create", "Album", FormMethod.Post, new { enctype = "multipart/form-data" })) { @Html.AntiForgeryToken() <div class="form-horizontal"> <h4>AlbumViewModel</h4>

为什么在使用IEnumerable filebase时出错

我使用这个代码

@using (Html.BeginForm("Create", "Album", FormMethod.Post, new { enctype = "multipart/form-data" })) 
{
  @Html.AntiForgeryToken()

  <div class="form-horizontal">
    <h4>AlbumViewModel</h4>
    <hr />
    @Html.ValidationSummary(true, "", new { @class = "text-danger" })
    <div class="form-group">
        @Html.LabelFor(model => model.Name, htmlAttributes: new { @class = "control-label col-md-2" })
        <div class="col-md-10">
            @Html.EditorFor(model => model.Name, new { htmlAttributes = new { @class = "form-control" } })
            @Html.ValidationMessageFor(model => model.Name, "", new { @class = "text-danger" })
        </div>
    </div>

    <div class="form-group">
        @Html.LabelFor(model => model.Toltip, htmlAttributes: new { @class = "control-label col-md-2" })
        <div class="col-md-10">
            @Html.EditorFor(model => model.Toltip, new { htmlAttributes = new { @class = "form-control" } })
            @Html.ValidationMessageFor(model => model.Toltip, "", new { @class = "text-danger" })
        </div>
    </div>

    <div class="form-group">
        @Html.LabelFor(model => model.Imageurl1, htmlAttributes: new { @class = "control-label col-md-2" })
        <div class="col-md-10">
            <input type="file" name="Files" value=" " id="File1" />
        </div>
    </div>

    <div class="form-group">
        @Html.LabelFor(model => model.Imageurl2, htmlAttributes: new { @class = "control-label col-md-2" })
        <div class="col-md-10">
            <input type="file" name="Files" value=" " id="File2" />
        </div>
    </div>

    <div class="form-group">
        @Html.LabelFor(model => model.Imageurl3, htmlAttributes: new { @class = "control-label col-md-2" })
        <div class="col-md-10">
            <input type="file" name="Files" value=" " id="File3" />
        </div>
    </div>`
@使用(Html.BeginForm(“创建”、“相册”、FormMethod.Post、新的{enctype=“multipart/formdata”}))
{
@Html.AntiForgeryToken()
相册视图模型

@Html.ValidationSummary(true,“,new{@class=“text danger”}) @LabelFor(model=>model.Name,htmlAttributes:new{@class=“controllabel col-md-2”}) @EditorFor(model=>model.Name,new{htmlAttributes=new{@class=“form control”}) @Html.ValidationMessageFor(model=>model.Name,“,new{@class=“text danger”}) @LabelFor(model=>model.Toltip,htmlAttributes:new{@class=“controllabel col-md-2”}) @EditorFor(model=>model.Toltip,new{htmlAttributes=new{@class=“form control”}) @Html.ValidationMessageFor(model=>model.Toltip,“,new{@class=“text danger”}) @LabelFor(model=>model.Imageurl1,htmlAttributes:new{@class=“controllabel col-md-2”}) @LabelFor(model=>model.Imageurl2,htmlAttributes:new{@class=“controllabel col-md-2”}) @LabelFor(model=>model.Imageurl3,htmlAttributes:new{@class=“controllabel col-md-2”}) `
这是控制器中的代码

[HttpPost]
public ActionResult Create(AlbumViewModel viewModel, IEnumerable<HttpPostedFileBase> files)
{
    var file = files.ToList();
    if (file[1].ContentLength>0)
    {
        viewModel.Imageurl1 = file[1].FileName;
    }
    if (file[2].ContentLength > 0)
    {
        viewModel.Imageurl2 = file[2].FileName;
    }
    if (file[3].ContentLength > 0)
    {
        viewModel.Imageurl3 = file[3].FileName;
    }
    return View();
}
[HttpPost]
公共操作结果创建(AlbumViewModel、IEnumerable文件)
{
var file=files.ToList();
如果(文件[1]。ContentLength>0)
{
viewModel.Imageurl1=文件[1]。文件名;
}
如果(文件[2]。ContentLength>0)
{
viewModel.Imageurl2=文件[2]。文件名;
}
如果(文件[3]。ContentLength>0)
{
viewModel.Imageurl3=文件[3]。文件名;
}
返回视图();
}
但我总是有错误

列表的索引从0开始,因此除非列表中有多个文件,否则将收到空引用异常

(file[0].ContentLength>0)...

请将视图标记减少为唯一有意义的部分。并将异常stacktrace作为文本发布。消息表明
文件
为空。几乎所有
NullReferenceException
的情况都是相同的。请参阅“”以获取一些提示。您的屏幕截图显示
if(文件[1]…
,但您的代码是
if(文件[1]…
!是哪一个?根据代码。我如何控制空间量?我用文件[0]开始索引,但再次出现错误。在这种情况下,您将得到
索引超出范围的
异常(不是
空引用
异常使用索引
[1]
如果列表中只有一个元素,则会导致
索引自动失效异常
。如果我的文件为null或为空,则我有代码检查我的文件[0]或文件[1],如果我的文件有值,则返回false以字符串形式保存文件名