2

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!

Jordan Carter
  • 1,276
  • 3
  • 19
  • 43
  • Likely, your `testContext.Entry(testimonial)` is `null` or the `testimonial` is `null` – Ian Feb 29 '16 at 03:29
  • My database table only has a category for id and testimonial. The testimonial edit is being captured in a text input. Could the issue be that it is changing the id to null? – Jordan Carter Feb 29 '16 at 03:30
  • 1
    `testimonial` is null. – Rob Feb 29 '16 at 03:33
  • Sorry, how would I go about making it not null? testimonial is an object of the testimonials model, which contains properties of the database table. I thought it would automatically grab what I've inputted in the text input. – Jordan Carter Feb 29 '16 at 03:34

1 Answers1

4

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.

Jordan Carter
  • 1,276
  • 3
  • 19
  • 43