0

I have 2 tables in my DB: "Competitions" and "CompetitionAnswers". Each competition will always have 3 answers. I want to be able to edit the Competition and CompetitionAnswers for the same view. I have managed to get the view to work by using editor templates

So this is my 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 this is my 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 to List", "Index")
</div>

When I hit the save button, I need the controller to update the answers in the answers table. At the moment, I just have the code that is generated by Visual Studio. I presume I have to loop through the answers and update them, but I'm not sure how to do this.

Here is my controller code:

Function Edit(competition As Competition) As ActionResult
        If ModelState.IsValid Then

            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 help would be appreciated. I have been searching on Google for 2 days now.

Josh Darnell
  • 11,304
  • 9
  • 38
  • 66

1 Answers1

0

When you inspect competition.Answers, do you see values in that property? If so, you will have to add them manually like this:

db.Answers.Add(competition.Answers[0]);

Also, check out this link with info on differences between Attach and Add

Entity Framework 4 - AddObject vs Attach

Since you said that comptetitions will always have 3 answers, here is an alternate solution: When you create a "Competition", you could create 3 CompetitionAnswers (with a flag that marks them as unanswered). Then, your edit code should work just fine.

Keith Stein
  • 6,235
  • 4
  • 17
  • 36
AlbertVo
  • 772
  • 4
  • 9
  • I thought db.Answers.Add(competition.Answers[0]); was used if you are creating a new record. These records already exist in the database. Within my edit function I can loop through the answers and can see their values. – Mike Boulton Mar 08 '12 at 07:11