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