我的数据库中有2个表:“竞赛”和“竞赛响应”。每场比赛总有3个答案。我希望能够为同一视图编辑Competition和CompetitionAnswers。我已设法通过使用编辑器模板
来使视图工作所以这是我的编辑模板:
@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>
这是我的观点
@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>
当我点击保存按钮时,我需要控制器更新答案表中的答案。目前,我只拥有Visual Studio生成的代码。我认为我必须遍历答案并更新它们,但我不知道该怎么做。
这是我的控制器代码:
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
任何帮助将不胜感激。我已经在Google上搜索了2天了。
答案 0 :(得分:0)
当你检查比赛时。答案,你看到该物业的价值吗?如果是这样,您将必须手动添加它们:
db.Answers.Add(competition.Answers [0]);
另外,请查看此链接,其中包含有关“附加”和“添加”之间差异的信息
Entity Framework 4 - AddObject vs Attach
既然你说comptetitions总会有3个答案, 这是另一种解决方案: 当您创建“竞争”时,您可以创建3个CompetitionAnswers(带有标记为未答复的标记)。然后,您的编辑代码应该可以正常工作。