tags:

views:

205

answers:

2

Hi,

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

A: 
 Survey updateSurvey;

 if (id == 0)
 {
     updateSurvey = new Survey();
 }
 else
 {
     updateSurvey = surveyRepository.GetSingle(Convert.ToInt32(id));
 }
 try
 {
   ..etc
Robert Harvey
yes I could do that but it doesn't solve my problem. My Survey and SurveyQuestions are all working properly, its only the SurveyQuestionOptions where it always adds a new SurveyQuestionOption record.
Joe
Make sure that, in your GetSingle method, you are also retrieving the SurveyQuestionOptions from the database on edit.
Robert Harvey
as above the SurveyQuestionOptions are being returned and appear to be updated in UpdateModel.. just SubmitChanges that creates new..
Joe
A: 

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.

Omar
I could do them individually but would like to use UpdateModel for simplicities sake. As I say it appears to update all the proper children and only when i callsSubmitChanges does it save new records. Also, does your code manage scenarios where a user removes an element from a list?
Joe
Maybe another thing to check is that your second level child is loaded, yoo must do an eager load on all child elements
Omar
@Joe: Nop, it doesn't, but it could be added, it would be the opposite if current is not null instead of creating a new one just mark it for deletion surveyRepository.Delete(current);
Omar
Hi again, sorry SO isn't very good at telling you when you have a comment. The second level of children has IsLoaded = true so why does linq create new ??
Joe