2012-03-07 117 views
0

我在我的DB中有2個表:「競賽」和「CompetitionAnswers」。每場比賽總會有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

當你檢查competition.Answers,你看到在該屬性的價值?如果是這樣,你將不得不像這樣手動添加它們:

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

此外,檢查用信息這個環節上附加和添加

Entity Framework 4 - AddObject vs Attach

既然你說comptetitions總會有3個答案之間的差異, 這裏是一個替代的解決方案: 當你創建是「比賽」,您可以創建3個CompetitionAnswers(帶有標記爲未答覆的標誌)。然後,你的編輯代碼應該工作得很好。

+0

我以爲db.Answers.Add(competition.Answers [0]);如果你正在創建一個新的記錄被使用。這些記錄已經存在於數據庫中。在我的編輯功能中,我可以循環查看答案並可以看到它們的值。 – 2012-03-08 07:11:08