ASP.NET edit action will not save -
i have website used complete health , safety self assessments when go edit assessment not save edited data. 1 have , ideas?
this 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 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 list", "index") </div>
and 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> }
i have figured out :)
if (modelstate.isvalid) { db.entry(wsassessment).state = entitystate.modified; db.savechanges(); return redirecttoaction("index"); } return view(wsassessment);
this part of code wrong wsassessmentanswers not being db.entry. here code 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);
Comments
Post a Comment