SaveObject() is always failing.

SaveObject() is always failing.

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


msrs_it posted on Thursday, October 20, 2011

Hi,

I'm trying to save my business object using the below code

public ActionResult Create()

{

ViewData.Model = Representative.NewRepresentative();

return PartialView("_CreateRepresentative");

}

 

[HttpPost]

public ActionResult Create(Representative representative, FormCollection collection)

{

if (ModelState.IsValid)

        {

      ValidateModel(representative);

                if (SaveObject(representative, false))

                return PartialView("_RepresentativeList");

        }

return PartialView("_CreateRepresentative");

}

The Issue is ModelState.IsValid is returning true and the ValidateModel is also not raising any errors. The BO representative metadata contains true for all of these properties IsValid, IsSelfValid, IsNew, IsDirty, and IsSavable. The body of if condition "SaveObject" is not executing.

I have placed a break point at first if condition and debug the whole process but the SaveObject is not saving the BO.

I appreciate your help.

Thanks & Regards,

SreeRam.

msrs_it replied on Thursday, November 24, 2011

This issue was raised because of calling UpdateModel(item) in the SaveObject method.

Even though the below code is not solution but a simple work around.

 

public ActionResult Create(Company company)

        {

            try

            {

                ViewData.Model = company.Save(false);

                return RedirectToAction("Confirmation", "Wizard");

            }

            catch (Csla.DataPortalException dpex)

            {

                if (dpex.BusinessException != null)

                    ModelState.AddModelError("", dpex.BusinessException.Message);

                else

                {

                    ModelState.AddModelError("", dpex.Message);

                }

                return View();

            }

            catch (Exception ex)

            {

                ModelState.AddModelError("", ex.Message);

                return View();

            }

        }

 

 

Still we can continue using the SaveObject() method if we set any private set properties of the business object.

Copyright (c) Marimer LLC