CSLA .NET

Vibrant discussion about CSLA .NET and using the framework to build great business applications.

Possible bug in code snippet from CSLA 4 MVC ebook

rated by 0 users
Not Answered This post has 0 verified answers | 6 Replies | 2 Followers

Top 500 Contributor
29 Posts
correodemarques posted on Thu, Nov 3 2011 1:52 PM

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?

 

All Replies

Top 500 Contributor
29 Posts

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.

Top 10 Contributor
9,270 Posts

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.

Rocky

Top 500 Contributor
29 Posts

RockfordLhotka:
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?

Top 10 Contributor
9,270 Posts

Yes, I think you are right, that call is superfluous.

Rocky

Top 500 Contributor
40 Posts

RockfordLhotka:

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

 

 

Top 10 Contributor
9,270 Posts

I think I missed your previous post. Added as a bug now:

http://www.lhotka.net/cslabugs/edit_bug.aspx?id=991

Rocky

Page 1 of 1 (7 items) | RSS

Copyright (c) 2006-2010 Marimer LLC. All rights reserved.
Email admin@lhotka.net for support.
Powered by Community Server (Non-Commercial Edition), by Telligent Systems