Value cannot be null. Parameter name: entity

16,055

Thanks for your help everyone. I figured out this way to fix it, based on you telling me what was null.

        [HttpPost, ActionName("Edit")]
    public ActionResult EditConfirmed(int id, string Testimonial)

    {
        TestimonialsContext testContext = new TestimonialsContext();
        Testimonials testimonial = testContext.testimonialContext.Find(id);
        testimonial.Testimonial = Testimonial;
        testContext.Entry(testimonial).State = EntityState.Modified;
        testContext.SaveChanges();
        return RedirectToAction("Index");
    }

Testimonial is the name of the input box, which is the same name as the entry in the database table.

Share:
16,055
Jordan Carter
Author by

Jordan Carter

Updated on June 04, 2022

Comments

  • Jordan Carter
    Jordan Carter almost 2 years

    I have the following view (basic "edit" template)

      @model SuccessStories.Models.Testimonials
    
    @using (Html.BeginForm())
    {
        @Html.AntiForgeryToken()
        
        <div class="form-horizontal">
            <h4>Testimonials</h4>
            <hr />
            @Html.ValidationSummary(true, "", new { @class = "text-danger" })
            @Html.HiddenFor(model => model.Id)
    
            <div class="form-group">
                @Html.LabelFor(model => model.Testimonial, htmlAttributes: new { @class = "control-label col-md-2" })
                <div class="col-md-10">
                    @Html.EditorFor(model => model.Testimonial, new { htmlAttributes = new { @class = "form-control" } })
                    @Html.ValidationMessageFor(model => model.Testimonial, "", new { @class = "text-danger" })
                </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>

    And the following actionresult in my controller:

    [HttpGet]
        public ActionResult Edit(int id)
        {
                TestimonialsContext testContext = new TestimonialsContext();
                Testimonials testimonials = testContext.testimonialContext.Find(id);
                return View(testimonials);  
        }
    
        [HttpPost]
        public ActionResult Edit(Testimonials testimonial)
        {
            TestimonialsContext testContext = new TestimonialsContext();
            testContext.Entry(testimonial).State = EntityState.Modified;
            testContext.SaveChanges();
    
            return RedirectToAction("Index");
        }
    

    The error is on this line:

    testContext.Entry(testimonial).State = EntityState.Modified;
    

    The error I get is "Value cannot be null. Parameter name: entity

    Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

    Exception Details: System.ArgumentNullException: Value cannot be null. Parameter name: entity"

    Please help. I've looked this up but can't find a solution that would work for me. Thanks!