1

Why do I have error when I use IEnumerable filebase?

I use this code

@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>`

and this code in Controller

[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();
}

but always I have error

http://i.stack.imgur.com/Fzvff.png

John Saunders
  • 157,405
  • 24
  • 229
  • 388
Nabi.M
  • 27
  • 7
  • Please reduce the view markup to only meaningful piece. And post the exception stacktrace as text. – abatishchev Dec 08 '14 at 03:42
  • 2
    The message suggests that `file` is null. –  Dec 08 '14 at 03:48
  • Almost all cases of `NullReferenceException` are the same. Please see "[What is a NullReferenceException in .NET?](http://stackoverflow.com/questions/4660142/what-is-a-nullreferenceexception-in-net)" for some hints. – John Saunders Dec 08 '14 at 04:59
  • 1
    Your screen shot shows `if (files[1]...` but you code is `if(file[1]...`! Which is it? –  Dec 08 '14 at 04:59
  • According code. How do I control the amount of space? – Nabi.M Dec 08 '14 at 05:44

1 Answers1

-2

The index for List<T> starts at 0, so unless you have more than one file in the list, you will receive a null reference exception.

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