asp.net - Update a child table in Entity Framework -


i have 2 tables in db: "competitions" , "competitionanswers". each competition have 3 answers. want able edit competition , competitionanswers same view. have managed view work using editor templates

so editor template:

 @modeltype golfbug.answer  <div class="editor-label left">  answer:<br />  </div> <div class="editor-field right"> @html.hiddenfor(function(model) model.competitionid) @html.hiddenfor(function(model) model.competitionanswersid) @html.editorfor(function(model) model.competitionanswer) </div> <div class="clear"></div> 

and view

@modeltype golfbug.competition  @code viewdata("title") = "edit" end code  <h2>edit</h2>  <script src="@url.content("~/scripts/jquery.validate.min.js")" type="text/javascript">       </script> <script src="@url.content("~/scripts/jquery.validate.unobtrusive.min.js")"    type="text/javascript"></script>  @using html.beginform() @html.validationsummary(true) @<fieldset>     <legend>competition</legend>      @html.hiddenfor(function(model) model.competitionid)      <div class="editor-label left">         @html.labelfor(function(model) model.clientid, "client")     </div>     <div class="editor-field right">         @html.dropdownlist("clientid", string.empty)         @html.validationmessagefor(function(model) model.clientid)     </div>     <div class="clear"></div>     <div class="editor-label left">         @html.labelfor(function(model) model.startdate)     </div>     <div class="editor-field right">         @html.editorfor(function(model) model.startdate)         @html.validationmessagefor(function(model) model.startdate)     </div>       <div class="clear"></div>     <div class="editor-label left">         @html.labelfor(function(model) model.competitionname)     </div>     <div class="editor-field right">         @html.editorfor(function(model) model.competitionname)         @html.validationmessagefor(function(model) model.competitionname)     </div>       <div class="clear"></div>     <div class="editor-label left">         @html.labelfor(function(model) model.competitiondetails)     </div>     <div class="editor-field right">         @html.editorfor(function(model) model.competitiondetails)         @html.validationmessagefor(function(model) model.competitiondetails)     </div>       <div class="clear"></div>     <div class="editor-label left">         @html.labelfor(function(model) model.compettionquestion)     </div>     <div class="editor-field right">         @html.editorfor(function(model) model.compettionquestion)         @html.validationmessagefor(function(model) model.compettionquestion)     </div>     <div class="clear"></div>      @html.editorfor(function(model) model.answers)        <p>         <input type="submit" value="save" />     </p> </fieldset>  end using  <div> @html.actionlink("back list", "index") </div> 

when hit save button, need controller update answers in answers table. @ moment, have code generated visual studio. presume have loop through answers , update them, i'm not sure how this.

here controller code:

function edit(competition competition) actionresult         if modelstate.isvalid              db.competitions.attach(competition)             db.objectstatemanager.changeobjectstate(competition, entitystate.modified)              db.savechanges()              return redirecttoaction("index")         end if          viewbag.clientid = new selectlist(db.clients, "clientid", "clientname", competition.clientid)         return view(competition)     end function 

any appreciated. have been searching on google 2 days now.

when inspect competition.answers, see values in property? if so, have add them manually this:

db.answers.add(competition.answers[0]);

also, check out link info on differences between attach , add

entity framework 4 - addobject vs attach

since said comptetitions have 3 answers, here alternate solution: when create "competition", create 3 competitionanswers (with flag marks them unanswered). then, edit code should work fine.


Comments

Popular posts from this blog

jasper reports - Fixed header in Excel using JasperReports -

media player - Android: mediaplayer went away with unhandled events -

python - ('The SQL contains 0 parameter markers, but 50 parameters were supplied', 'HY000') or TypeError: 'tuple' object is not callable -