开发者

MVC Updatemodel and Linq-To-Sql creates new entity instead of updating

开发者 https://www.devze.com 2022-12-13 01:43 出处:网络
I have a survey form with the following data hierarchy survey -< surveyQuestions -< surveyQuestionOptions

I have a survey form with the following data hierarchy

survey -< surveyQuestions -< surveyQuestionOptions

I use updateModel to update the Survey entity in my save method which works fine for survey and surveyQuestions, however surveyQuestionOptions appear updated when I examine the updateSurvey variable in debugger but after I call SubmitChanges I get new surveyQuestionOption records instead of updates. My开发者_StackOverflow code is as follows

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)%>

Controller

[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();}

    }

Any help is appreciated


 Survey updateSurvey;

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


Mmm.. i have not tried to Update directly a child element like that, specially with Complex models (various depth levels). I mostly use a ViewModel and then use that ViewModel to Update the Actual Model(the LinqtoSQL classes).

I would Update the child's this way:

Get The Current Saved Survey

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;
    //...
   }
   }

I know this is not the depth level with the problem but is the same concept.


I've tried with LoadOptions in LINQ to SQL:

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();

Didn't help. All existing ContactEmail is deleted and inserted again.

0

精彩评论

暂无评论...
验证码 换一张
取 消