新的實體,我有以下數據層次的調查形式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();}
}
任何幫助表示讚賞
我想這也是一個答案。 – 2010-12-23 00:02:00