2016-05-02 4 views
0

Net Ich habe versucht, eine Bearbeitung für das Hochladen eines Bildes, aber jetzt, wenn ich es bearbeite, stürzt ab und gibt" System.ArgumentNullException "in System aufgetreten .Core.dll wurde aber nicht im Benutzercode behandelt '. Das alles begann, als ich versuchte, eine Bearbeitung für den Bildupload zu implementieren. Es stürzt auf der edit.cshtml-Seite ab, ich habe einen Kommentar dort platziert, wo er abstürzt. Jede Hilfe wäre wirklich zu schätzen, wenn Sie weitere Informationen benötigen Sie michSystem.ArgumentNullException "in System.Core.dll aufgetreten ist aber nicht in Benutzercode

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

Bearbeiten Ansicht

<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="~/[email protected](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") 
} 

Modell

//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; } 
} 
+0

In welcher Zeile wirft der Code eine Ausnahme? Was wird im Stack-Trace aufgerufen? Etwas ist null, wo es nicht sein sollte, aber es ist schwierig, Fehler wie diese zu lösen, ohne mehr darüber zu wissen, wo gerade Dinge explodieren. –

+0

es führt den Fehler im Bearbeitungsmodus in dieser Zeile // Abstürze hier @if (Model.Files.Any (f => f.FileType == FileType.Avatar)) {

Current Avatar
avatar
} – Newbie

+0

und in Stack-Trace: [ArgumentNullException: Der Wert darf nicht null sein. Parametername: source]. Ich bin neu, daher bin ich mir nicht sicher, ob ich die relevanten Informationen zur Verfügung gestellt habe. @ MADsc13nce – Newbie

Antwort

1

Der Fehler wissen lassen, dass Sie Es wird vorgeschlagen, dass Sie eine Methode mit einem Null-Argument irgendwo in Ihrem Code aufrufen . Da es mit der if-Anweisung abstürzt, die mit Model.Files.Any(f => f.FileType == FileType.Avatar) beginnt, würde ich überprüfen, dass Model.Files vor dem Fortfahren nicht null ist.

Der Code könnte wie folgt aussehen:

@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="~/[email protected](f => f.FileType == FileType.Avatar).FileId" alt="avatar" /> 
     </div> 
    </div> 
} 

Wenn Model.Files sollte nie Null sein, dann könnten Sie untersuchen müssen, warum dieser Wert auch nicht eingestellt werden.

Verwandte Themen