0

I have a website that is used to complete health and safety self assessments but when you go to edit an assessment it will not save the edited data. Any one have and ideas?

This is my controller:

// GET: /WSAssessment/Edit

public ActionResult Edit(int id = 0)
{
    WSAssessment wsassessment = db.WSAssessments.Find(id);
    if (wsassessment == null)
    {
        return HttpNotFound();
    }
    var view = View(wsassessment);

    view.ViewBag.Origin = "Edit";

    return view;
}

//
// POST: /WSAssessment/Edit

[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Edit(WSAssessment wsassessment)
{
    ModelState.Clear();


    if (ModelState.IsValid)
    {              
        db.Entry(wsassessment).State = EntityState.Modified;
        db.SaveChanges();
        return RedirectToAction("Index");
    }
    return View(wsassessment);
}

This is my view:

@model Healthy_and_Safety_Website.Models.WSAssessment

@{
    ViewBag.Title = "Edit";
}

<h2>Edit</h2>

@using (Html.BeginForm())
{
    @Html.AntiForgeryToken()
    @Html.ValidationSummary(true)
<fieldset>
    <legend>WSAssessment</legend>


    @Html.HiddenFor(model => model.ID)
    @Html.ValidationSummary(true)

    <div class="editor-label">
        @Html.LabelFor(model => model.Username)
    </div>
    <div class="editor-field">
        @Html.EditorFor(model => model.Username)
        @Html.ValidationMessageFor(model => model.Username)
    </div>

    <div class="editor-label">
        @Html.LabelFor(model => model.Date)
    </div>
    <div class="editor-field">
        @Html.EditorFor(model => model.Date)
        @Html.ValidationMessageFor(model => model.Date)
    </div>

    <div class="editor-label">
        @Html.LabelFor(model => model.Workstation)
    </div>
    <div class="editor-field">
        @Html.EditorFor(model => model.Workstation)
        @Html.ValidationMessageFor(model => model.Workstation)
    </div>

    <section id="EditSection">
        <table style="width:0%">
            <tr>
                <th>Question</th>
                <th>Yes</th>
                <th>Problem?</th>
                <th>Suggested Solution</th>
                <th>Complete?</th>
                <th>Completed By</th>
                <th>Completion Notes</th>
            </tr>

            <tr>
                <td>
                    @Html.EditorFor(m => m.WSAssessmentAnswers)
                </td>
            </tr>

        </table>
    </section>

    <p>
        <input type="submit" value="Save" />
    </p>
</fieldset>
}

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

And finally the WSAssessmentAnswer model:

@if (@ViewBag.Origin == "Edit")
{
    <tr class="WSAssessmentTableQuestion">
        <td>
            @Html.ValueFor(model => model.WSAssessmentTemplateQuestion.Question)
        </td>
        <td>
            <div class="editor-field">@Html.CheckBoxFor(model => model.Answer)</div>
        </td>
         <td>
            <div class="editor-field">@Html.CheckBoxFor(model => model.Problem)</div>
        </td>
        <td>
            <div class="editor-field">@Html.TextAreaFor(model => model.ActionRequired)</div>
            @Html.ValidationMessageFor(model => model.ActionRequired)
            @Html.HiddenFor(model => model.WSAssessmentQuestionID)
            @Html.HiddenFor(model => model.Question)
            @Html.HiddenFor(model => model.Guidance)
            @Html.HiddenFor(model => model.WSAssessmentID)
            @Html.HiddenFor(model => model.ID)
        </td>

        <td class="WSAssessmentAdminColumn">
            <div class="editor-field">@Html.CheckBoxFor(model => model.ActionComplete)</div>

        </td>
        <td class="WSAssessmentAdminColumn">
            <div class="editor-field">@Html.EditorFor(model => model.ActionCompleteBy)</div>
        </td>
        <td class="WSAssessmentAdminColumn">
            <div class="editor-field">@Html.TextAreaFor(model => model.ActionCompleteNotes)</div>
        </td>

    </tr>
}
DavidG
  • 113,891
  • 12
  • 217
  • 223
CS-JCB
  • 1
  • 3
  • What is happening? When you press save is your `ModelState` valid? Because your area clearing your modelstate... – Jamie Rees Jul 08 '15 at 10:39
  • As far as I know it is, but the clear does not seem to affect it, it doesn't save either way – CS-JCB Jul 08 '15 at 10:44
  • 1
    Is the `db.SaveChanges` line getting called at all? Can you confirm that? Are you sure that the ModelState is valid? – DavidG Jul 08 '15 at 10:45
  • `if (ModelState.IsValid) { db.Entry(wsassessment).State = EntityState.Modified; db.SaveChanges();` – CS-JCB Jul 08 '15 at 10:47
  • That's just pasting the code from the question, I'm asking if `ModelState.IsValid` is true or not. Can you step through the code and see if it gets past that line? – DavidG Jul 08 '15 at 10:49
  • I ran through the debugger line by line and it says that the `ModelState.IsValid` is true – CS-JCB Jul 08 '15 at 10:54
  • And are the properties of the `wsassessment` object what you expect them to be? – DavidG Jul 08 '15 at 10:57
  • I am not sure, how do I find out? (sorry for being stupid) – CS-JCB Jul 08 '15 at 11:02
  • Hover over the value in code when debugging. – DavidG Jul 08 '15 at 11:03
  • Yes, it has all the properties I expect it to have – CS-JCB Jul 08 '15 at 11:06
  • Can you try and specify what properties are being changed? e.g. Top answer on this post: http://stackoverflow.com/questions/15336248/entity-framework-5-updating-a-record – Jamie Rees Jul 08 '15 at 11:37
  • Is this what you want? I am trying to save the edited properties of: model.Answer model.Problem model.ActionRequired model.ActionComplete model.ActionCompleteBy model.ActionCompleteNotes (basically look at WSAssessmentAwnser Model) – CS-JCB Jul 08 '15 at 12:43
  • I have noticed that it is saving but only the WSAssessments not the WSAssessmentAnswers as well. Does anyone know how I would save the WSAssessmentAnswers, it is a separate table in the SQL database – CS-JCB Jul 09 '15 at 08:53

1 Answers1

0

I have figured it out :)

if (ModelState.IsValid)
{              
    db.Entry(wsassessment).State = EntityState.Modified;
    db.SaveChanges();
    return RedirectToAction("Index");
}
return View(wsassessment);

This part of code was wrong as the WSAssessmentAnswers were not being db.Entry. Here is the code that works:

if (ModelState.IsValid)
            {
                foreach (var item in wsassessment.WSAssessmentAnswers)
                {
                    db.Entry(item).State = EntityState.Modified;
                }

                db.Entry(wsassessment).State = EntityState.Modified;
                db.SaveChanges();
                return RedirectToAction("Index");
            }
            return View(wsassessment);
CS-JCB
  • 1
  • 3