0

Net I was trying to do a Edit for uploading a image but now whenever I edit it crashes and gives 'System.ArgumentNullException' occurred in System.Core.dll but was not handled in user code'. This all started when I tried to implement an edit for the image upload. It crashes on the edit.cshtml page, I have placed a comment right where it crashes. Any help would be really appreciated, if you require any more information please let me know

AnimalsController

  [HttpPost]
    [ValidateAntiForgeryToken]
    public ActionResult Edit([Bind(Include = "LatinNameID,CommonNameID,LatinName,CommonName,GenusID,SpeciesID,Description,FamilyID,CountryID,ContinentID,OrderID")] Animal animal, HttpPostedFileBase upload,string latinID,string commonID)
    {

        var animalToUpdate = db.Animals.Find(latinID,commonID);

        //Does a check to see if entry exists in database
        if ((db.Animals.Any(ac => ac.LatinName.Equals(animal.LatinName))) || (db.Animals.Any(ac => ac.CommonName.Equals(animal.CommonName))))
        {
            ModelState.AddModelError("LatinName", "Already Exists");
            ModelState.AddModelError("CommonName", "Duplicate Entry");
        }
        else
        {
            if (ModelState.IsValid)
            {
                if (upload != null && upload.ContentLength > 0)
                {
                    if (animalToUpdate.Files.Any(f => f.FileType == FileType.Avatar))
                    {
                        db.File.Remove(animalToUpdate.Files.First(f => f.FileType == FileType.Avatar));
                    }
                    var avatar = new File
                    {
                        FileName = System.IO.Path.GetFileName(upload.FileName),
                        FileType = FileType.Avatar,
                        ContentType = upload.ContentType
                    };
                    using (var reader = new System.IO.BinaryReader(upload.InputStream))
                    {
                        avatar.Content = reader.ReadBytes(upload.ContentLength);
                    }
                    animalToUpdate.Files = new List<File> { avatar };
                }
                db.Entry(animal).State = EntityState.Modified;
                db.SaveChanges();
                return RedirectToAction("Index");
            }
        }

        ViewBag.ContinentID = new SelectList(db.Continents, "ContinentID", "ContinentName", animal.ContinentID);
        ViewBag.CountryID = new SelectList(db.Countries, "CountryID", "CountryName", animal.CountryID);
        ViewBag.FamilyID = new SelectList(db.Families, "FamilyID", "FamilyName", animal.FamilyID);
        ViewBag.GenusID = new SelectList(db.Genus, "GenusID", "GenusName", animal.GenusID);
        ViewBag.OrderID = new SelectList(db.Orders, "OrderID", "OrderName", animal.OrderID);
        ViewBag.SpeciesID = new SelectList(db.Species, "SpeciesID", "SpeciesName", animal.SpeciesID);
        return View(animal);
    }

Edit View

<h2>Edit</h2>


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

<div class="form-horizontal">
    <h4>Animal</h4>
    <hr />
    @Html.ValidationSummary(true, "", new { @class = "text-danger" })
    @Html.HiddenFor(model => model.LatinNameID)

    @Html.HiddenFor(model => model.CommonNameID)

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

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

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

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

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

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

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

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

    <div class="form-group">
        @Html.LabelFor(model => model.OrderID, "OrderID", htmlAttributes: new { @class = "control-label col-md-2" })
        <div class="col-md-10">
            @Html.DropDownList("OrderID", null, htmlAttributes: new { @class = "form-control" })
            @Html.ValidationMessageFor(model => model.OrderID, "", new { @class = "text-danger" })
        </div>
    </div>
    //Crashes here
    @if (Model.Files.Any(f => f.FileType == FileType.Avatar))
    {
        <div class="form-group">
            <span class="control-label col-md-2"><strong>Current Avatar</strong></span>
            <div class="col-md-10">
                <img src="~/File?id=@Model.Files.First(f => f.FileType == FileType.Avatar).FileId" alt="avatar" />
            </div>
        </div>
    }


    <div class="form-group">
        @Html.Label("Avatar", new { @class = "control-label col-md-2" })
        <div class="col-md-10">
            <input type="file" id="Avatar" name="upload" />
        </div>
    </div>

    <div class="form-group">
        <div class="col-md-offset-2 col-md-10">
            <input type="submit" value="Save" class="btn btn-default" />
        </div>
    </div>
</div>
}

<div>
@Html.ActionLink("Back to List", "Index")
</div>

@section Scripts {
@Scripts.Render("~/bundles/jqueryval")
}

Model

//Animal
public class Animal
{
    [Key, Column(Order = 0)]
    public string LatinNameID { get; set; }
    public string LatinName { get; set; }

    [Key, Column(Order = 1)]
    public string CommonNameID { get; set; }
    public string CommonName { get; set; }

    public string GenusID { get; set; }

    public string SpeciesID { get; set; }

    public string Description { get; set; }

    public string FamilyID { get; set; }

    public string CountryID { get; set; }

    public string ContinentID { get; set; }

    public string OrderID { get; set; }

    public virtual Continent Continent { get; set; }

    public virtual Genus Genus { get; set; }

    public virtual Species Species { get; set; }

    public virtual Family Family { get; set; }

    public virtual Country Country { get; set; }

    public virtual Order Order { get; set; }

    public virtual ICollection<File> Files { get; set; }
}

//File
 public class File
{
    public int FileId { get; set; }
    [StringLength(255)]
    public string FileName { get; set; }
    [StringLength(100)]
    public string ContentType { get; set; }
    public byte[] Content { get; set; }
    public FileType FileType { get; set; }
    public string LatinNameID { get; set; }
    public string CommonNameID { get; set; }
    public virtual Animal Animal { get; set; }
}
tereško
  • 58,060
  • 25
  • 98
  • 150
Newbie
  • 61
  • 3
  • 10
  • What line is the code throwing an exception at? What is being called in the stack trace? Something is null where it shouldn't be, but it's difficult to solve errors like these without more knowledge about where specifically things are blowing up. – Matt Dalzell May 02 '16 at 16:37
  • it throws the error in Edit View at this line //Crashes here @if (Model.Files.Any(f => f.FileType == FileType.Avatar)) {
    Current Avatar
    avatar
    }
    – Newbie May 02 '16 at 16:44
  • and in the stack trace : [ArgumentNullException: Value cannot be null. Parameter name: source]. I am new to this so I am unsure if I provided the relevant information. @MADsc13nce – Newbie May 02 '16 at 16:56

1 Answers1

1

The error that you are getting suggests that you are calling a method with a null argument somewhere in your code. Since it crashes with the if statement beginning with Model.Files.Any(f => f.FileType == FileType.Avatar), I would verify that Model.Files is not null before proceeding.

The code could look like the following:

@if (Model.Files != null && Model.Files.Any(f => f.FileType == FileType.Avatar))
{
    <div class="form-group">
        <span class="control-label col-md-2"><strong>Current Avatar</strong></span>
        <div class="col-md-10">
            <img src="~/File?id=@Model.Files.First(f => f.FileType == FileType.Avatar).FileId" alt="avatar" />
        </div>
    </div>
}

If Model.Files should never be null, then you might need to investigate why that value is not being set as well.

Matt Dalzell
  • 775
  • 3
  • 14