I was looking to this code in page 18 of Using CSLA 4 ASP.NET MVC:
[HttpPost]
public ActionResult Edit(int id, ProjectEdit item)
{
try
{
LoadProperty(item, ProjectEdit.IdProperty, id);
ViewData.Model = item; UpdateModel(item);
ViewData.Model = item.Save(true);
return RedirectToAction("Index");
}
catch (Csla.DataPortalException ex)
{
if (ex.BusinessException != null)
ModelState.AddModelError("", ex.BusinessException.Message);
else
ModelState.AddModelError("", ex.Message);
return View(); }
catch
{
ModelState.AddModelError("", ex.Message);
return View();
}
}
And I was wondering, why is a call to UpdateModel() here:
ViewData.Model = item; UpdateModel(item);
if we are receiving the ProjectEdit object as a parameter and the DefaultBinder already is giving us that object populated with the form values?
I'm looking now at the implementation of Csla.Web.Mvc.Controller.SaveObject and I'm more confused than before. In this method UpdateModel() is called.
Is this necessary if we received our object via an action method parameter? That object is not already loaded with data from the binding process? What is the reason to call to UpdateModel again?
It looks like what I thought about the binding process is not right.
If I recall correctly (and I might not, because I'm on pain medication due to recent surgery - probably shouldn't be posting while impaire actually :) ), the challenge I'm highlighting here is that the Id property is read-only, but it needs to be set.
The model binder won't set read-only properties, so all other properties are set, but not that one. so LoadProperty is used to set that property, and then the model needs to be updated.
I was under the impression that when the method starts, you already have a business object with all the properties loaded through databinding (except the ID). Then you use LoadPropertyto to set the ID and after that you have the business object fully loaded and ready to be saved. Why do you have to call UpdateModel after that?
Yes, I think you are right, that call is superfluous.
Yes, I think you are right, that call is superfluous.
Hi Rocky,
As you stated that the call to UpdateModel(item); is superfluous, this is the case where my business object is always failing to save. Below is an example of my Create method in controller.
[HttpPost]
public ActionResult Create(Company company)
{
try
{
if (ModelState.IsValid)
if (SaveObject(company, false))
RedirectToAction("Confirmation", "Wizard");
return View();
}
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();
}
}
This is the same issue I've posted one month back
http://forums.lhotka.net/forums/p/10789/50325.aspx#50325
I've tried the same method without using SaveObject(). I've replaced the try block with the below code. Since I'm creating new company, because of no need to set any private set properties, I've skipped using UpdateModel(). The Bussiness object was saved successfully without any errors.
ViewData.Model = company.Save(false);
return RedirectToAction("Confirmation", "Wizard");
After confirming successful object save operation, I believe that this is a bug in the Controller.SaveObject() overloaded methods. This method works fine in Edit scenario but always failing in Create scenario.
So I continued with this work-around (without SaveObject()).
Let me know if this was fixed.
FYI: I'm using the latest beta release 4.2.1
I think I missed your previous post. Added as a bug now:
Copyright (c) Marimer LLC