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.