2009-11-30 48 views
4

新的實體,我有以下數據層次的調查形式MVC的UpdateModel和LINQ到SQL創建,而不是更新

調查 - < surveyQuestions - < surveyQuestionOptions

我使用的UpdateModel更新調查實體在我的保存方法中,對於調查和調查問題工作正常,但是當我在調試器中檢查updateSurvey變量時surveyQuestionOptions顯示爲更新,但在調用SubmitChanges後,我得到新的surveyQuestionOption記錄而不是更新。我的代碼如下

HTML

<%= Html.TextBox("Survey.Id", Model.Id)%> 
<%= Html.TextBox("Survey.SurveyName", Model. SurveyName)%> 

<%= Html.TextBox("Survey.SurveyQuestions[0].Id", Model.Id)%> 
<%= Html.TextBox("Survey.SurveyQuestions[0].Question", Model. Question)%> 

<%= Html.TextBox("Survey.SurveyQuestions[0].SurveyQuestionOptions[0].Id", Model.Id)%> 
<%= Html.TextBox("Survey.SurveyQuestions[0].SurveyQuestionOptions[0].Option", Model. Option)%> 

控制器

[AcceptVerbs(HttpVerbs.Post)] 
     public ActionResult Save(int? id, IList<ChannelForm> channelForms, FormCollection fc) 
     { 

      Survey updateSurvey = new Survey(); 

      //if this is an existing Surveyretrieve that record from the database ready for updating 
      if (id != 0) 
      { 
       updateSurvey = surveynRepository.GetSingle(Convert.ToInt32(id)); 
      } 

      try 
      { 
       // updateSurvey and all child elements 
       UpdateModel(updateSurvey, "Survey"); 
       surveyRepository.Save(); 
return View(); 
    }catch 
{return View();} 

    } 

任何幫助表示讚賞

+0

我想這也是一個答案。 – 2010-12-23 00:02:00

回答

0
Survey updateSurvey; 

if (id == 0) 
{ 
    updateSurvey = new Survey(); 
} 
else 
{ 
    updateSurvey = surveyRepository.GetSingle(Convert.ToInt32(id)); 
} 
try 
{ 
    ..etc 
+0

是的,我可以做到這一點,但它不能解決我的問題。我的調查問卷和調查問題都正常工作,它只是其中總是添加新的SurveyQuestionOption記錄的SurveyQuestionOptions。 – Joe 2009-12-01 09:05:30

+0

確保在您的GetSingle方法中,您還在編輯時從數據庫中檢索SurveyQuestionOptions。 – 2009-12-01 15:40:27

+0

與上述SurveyQuestionOptions正在返回並似乎更新UpdateModel ..只是SubmitChanges創建新.. – Joe 2009-12-04 15:10:49

0

嗯..我沒試過直接更新像一個子元素特別是複雜模型(不同深度級別)。我主要使用ViewModel,然後使用該ViewModel來更新實際模型(LinqtoSQL類)。

我會更新孩子的這種方式:

獲取當前保存的調查

currentSurvey = surveyRepository.GetSingle(Convert.ToInt32(id)); 


foreach (var option in updateSurveyViewModel.SurveyQuestions) 
{ 
    //check if exist 
    var current = currentSurvey.SingleOrDefault(a=> a.Id == option.Id); 
    if (current == null) 
    { 
    //Create a NewOne and attach it to the curentSurvey 
    } 
    else 
    { 
    //already exist, Update the option 
    current.Option = option.Option; 
    //... 
    } 
    } 

我知道這是不是有問題的深度級別,但是是同一個概念。

+0

我可以單獨做它們,但想要簡單起見,使用UpdateModel。正如我所說,似乎更新所有適當的孩子,只有當我調用SubmitChanges它會保存新的記錄。另外,您的代碼是否可以管理用戶從列表中刪除元素的場景? – Joe 2009-12-01 15:43:07

+0

也許另一件要檢查的是你的二級孩子被加載,你必須對所有子元素做一個急切的加載 – JOBG 2009-12-01 15:45:20

+0

@Joe:Nop,它沒有,但它可以被添加,如果當前是不是null而不是創建一個新的只需標記爲刪除surveyRepository.Delete(current); – JOBG 2009-12-01 15:49:43

0

我試着在LINQ to SQL LoadOptions:

var loadOptions = new System.Data.Linq.DataLoadOptions(); 
loadOptions.LoadWith<Contact>(c => c.ContactEmails); 
db.LoadOptions = loadOptions; 

Contact old = db.Contacts.SingleOrDefault(c => c.ContactID == id); 
UpdateModel(old, "Contact"); 

db.SubmitChanges(); 

沒有幫助。所有現有的ContactEmail都將被刪除並重新插入。