ASP.NET编辑操作不会保存

时间:2015-07-08 09:37:52

标签: asp.net asp.net-mvc asp.net-mvc-5 editor savechanges

我有一个用于完成健康和安全自我评估的网站,但是当您编辑评估时,它不会保存编辑的数据。任何人都有想法吗?

这是我的控制者:

// 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);
}

这是我的观点:

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

最后是WSAssessmentAnswer模型:

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

1 个答案:

答案 0 :(得分:0)

我已经弄明白了:)

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

这部分代码错误,因为WSAssessmentAnswers不是db.Entry。以下是有效的代码:

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);