Musango Wope
Musango Wope

Reputation: 11

Field in database table are nullified when record is edited in view

Every time I try to edit a record that I have created in my ASP.NET MVC project, when I click save the fields are nullified.

Here is part of the code for my Edit Get and post in my controller:

Review Controller:

// GET: Reviews/Edit/5
    public ActionResult Edit(int? id)
    {
        if (id == null)
        {
            return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
        }
        Review review = db.Reviews.Find(id);
        if (review == null)
        {
            return HttpNotFound();
        }
    ViewBag.ReviewID = new SelectList(db.Reviews, "ReviewID", "Rating", review.ReviewID);
    return View(review);
}


// POST: Reviews/Edit/5
// To protect from overposting attacks, please enable the specific properties you want to bind to, for 
// more details see http://go.microsoft.com/fwlink/?LinkId=317598.
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Edit([Bind(Include = "ReviewID,Username,WellnessService,Rating,Feedback,Date")] Review review)
{
    if (ModelState.IsValid)
    {
        db.Entry(review).State = EntityState.Modified;
        db.SaveChanges();
        return RedirectToAction("ReviewEdit");
    }
    ViewBag.ReviewID = new SelectList(db.Reviews, "ReviewID", "Rating", review.ReviewID);
    return View(review);
}

Here is the code for my view called Edit:

@using (Html.BeginForm())
        {
                @Html.AntiForgeryToken()
            <div class="form-horizontal">
                <h4>@Html.DisplayFor(model => model.WellnessService)</h4>
                <hr />
                @Html.ValidationSummary(true, "", new { @class = "text-danger" })
                @Html.HiddenFor(model => model.ReviewID)


                <div class="form-group">
                    @Html.LabelFor(model => model.Rating, htmlAttributes: new { @class = "control-label col-md-2" })
                    <div class="col-md-10" align="left">
                        @{
                            List<SelectListItem> listItems = new List<SelectListItem>();
                            listItems.Add(new SelectListItem { Text = "1", Value = "1" });
                            listItems.Add(new SelectListItem { Text = "2", Value = "2" });
                            listItems.Add(new SelectListItem { Text = "3", Value = "3" });
                            listItems.Add(new SelectListItem { Text = "4", Value = "4" });
                            listItems.Add(new SelectListItem { Text = "5", Value = "5" });
                            listItems.Add(new SelectListItem { Text = "6", Value = "6" });
                            listItems.Add(new SelectListItem { Text = "7", Value = "7" });
                        }

                        @Html.DropDownListFor(model => model.Rating, listItems, "Please choose value")
                        @Html.ValidationMessageFor(model => model.Rating, "", new { @class = "text-danger" })
                    </div>
                </div>

                <div class="form-group">
                    @Html.LabelFor(model => model.Feedback, htmlAttributes: new { @class = "control-label col-md-2" })
                    <div class="col-md-10">
                        @Html.TextAreaFor(model => model.Feedback, new { htmlAttributes = new { @class = "form-control" } })
                        @Html.ValidationMessageFor(model => model.Feedback, "", 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 My Reviews", "ReviewEdit", new { @class = "btn btn-success btn-lg" })
        </div>

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

Help would greatly be appreciated

Thanks in advance

Upvotes: 1

Views: 88

Answers (1)

Matt
Matt

Reputation: 1616

Don't have a dev pc to hand to check this, but are you sure review has data in it. If it has, can you be sure that the following line works?

db.Entry(review).State = EntityState.Modified; 

Don't you need to attach review to the context?

What happens if you temporarily do something like;

if (ModelState.IsValid)
{
    var foo = db.Entry.Where(x=>x.reviewId == review.reviewId).First();
    // then manually set all the parameters

    db.SaveChanges();
    return RedirectToAction("ReviewEdit");
}

Its not elegant, but if that works, and your data is updated, at least you know its the way you are attaching the object to the context. I'll try and get a better example later when I can get to a dev workstation.

Upvotes: 1

Related Questions