MVC3 Update Children

MVC3 Update Children

Old forum URL: forums.lhotka.net/forums/t/11145.aspx


xequence posted on Friday, February 10, 2012

Edit.

public ActionResult Edit(int id)

        {

            //var model = PressReleaseEdit.GetEditableRoot();

            //var model = PressReleaseEdit.AdminGetPressReleaseById(id);

 

            ViewData.Model = PressReleaseEdit.AdminGetPressReleaseById(id);

            // bind dropdownlist

            ViewBag.BoilerPlateList = new SelectList(BoilerPlateList.GetReadOnlyList(), "BoilerPlateId", "Name");

 

            return View();

        }

 

then the post

  public ActionResult Edit(PressReleaseEdit obj, int id, IEnumerable<HttpPostedFileBase> fileUpload, int? radioValue)

      {// do work}

 

The problem is, my OBJ is dirty even though I change no values. All of my children with this object are null except for the model values that exist on the form. How can I get it so my OBJ retains all the child objects?

RockfordLhotka replied on Friday, February 10, 2012

I assume you've read the Using CSLA 4: ASP.NET ebook?

When doing an update of an existing object you need to decide whether to get the object from Session, reload it from the database, or create an empty object.

You or the CslaModelBinder then loads the properties with values from the postback.

If you got the object from Session or the database you can then save it normally.

If you created an empty object, you need to use the forceUpdate parameter on the Save method to force it to save. It is also the case that you have no way to know if the object actually needs to be saved or not - it is assumed to be dirty, because the original values aren't known in this case (you didn't retrieve them for comparison).

xequence replied on Friday, February 10, 2012

Yes, I have the books and I even asked you a question at the conference in vegas about thresholds! 

 

As for the task at hand, you would say it is easier to put the object in TempData and perform the typical save like this?

if (SaveObject(obj, true))

                {

 

                    var item = ViewData.Model;

                    obj = (PressReleaseEdit)item;

                    return RedirectToAction("Success", new { id = obj.PressReleaseId });

                }

RockfordLhotka replied on Friday, February 10, 2012

I'm not sure I follow your question?

xequence replied on Friday, February 10, 2012

I am editing child data in the same form as the parent. So an example would be this...

@Html.EditorFor(model => model.Highlight.EndDate)

 

My child object is Highlight.

When I change the highlight start date, I want to update this record.

I am using a TempData object to persist my object like this...

 

public ActionResult Edit(int id, IEnumerable<HttpPostedFileBase> fileUpload, int? radioValue)

        {

            PressReleaseEdit obj = (PressReleaseEdit)TempData["obj"];

// etc

}

The problem, is that my childObject is not dirty even though I changed a value. 

 

Here is my child object.

public readonly static PropertyInfo<HighlightAssignmentEdit> HighlightProperty = RegisterProperty<HighlightAssignmentEdit>(p => p.Highlight);

        public HighlightAssignmentEdit Highlight

        {

            get

            {

                if (!(FieldManager.FieldExists(HighlightProperty)))

                    LoadProperty(HighlightProperty, DataPortal.CreateChild<HighlightAssignmentEdit>());

                return GetProperty(HighlightProperty);

            }

            set { SetProperty(HighlightProperty, value); }

        }

Copyright (c) Marimer LLC