Calling ViewModel with Business Object parameter

Calling ViewModel with Business Object parameter

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


cjherasmus posted on Thursday, June 19, 2014

I'm calling this code from my front-end to load a filtered read-only list:

Bxf.Shell.Instance.ShowView(typeof(Views.MyList).AssemblyQualifiedName, "myListViewSource", new ViewModels.MyList(), "Dashboard");

My viewmodel looks like this where the parameter is hard-coded:

public class MyList : ViewModel<NGIS.Business.MyList>

    {

        public Mylist()

        {

            ManageObjectLifetime = false;

            BeginRefresh(callback => MyList.Get(54930, callback));

        }

}

My view displays correct with the data.

When I change my calling code in the front-end to this. I'm now passing the parameter:

Bxf.Shell.Instance.ShowView(typeof(Views.MyList).AssemblyQualifiedName, "myListViewSource", new ViewModels.MyList(recordId), "Dashboard");

And change my viewmodel to look like this:

public class MyList : ViewModel<NGIS.Business.MyList>

    {

        public Mylist(int recordId)

        {

            ManageObjectLifetime = false;

            BeginRefresh(callback => MyList.Get(recordId, callback));

        }

}

My view is then not displayed. It seems that calling the ViewModel with a parameter doesn't work as expected.

What am I doing wrong here please?

Thanks

cjherasmus replied on Thursday, July 03, 2014

So it appears that I can't pass a parameter into the ViewModel constructor like I wanted to.

What options are left: dependency injection / properties / interfaceclass inbetween ?

What is the best option to use for CSLA?

skagen00 replied on Monday, July 07, 2014

We don't use a formal MVVM framework.  We extend ViewModelBase in CSLA, etc.

Pretty large Silverlight CSLA 4.x commercial app.

We frequently just have the user control invoke an "Initialize" method on the view model, passing the ID or whatever is needed.  That Initialize method does the BeginRefresh.

When dealing with a Silverlight application at this point, I think expediency should take precedence to ideals.  I wouldn't personally advocate trying to figure out some nuanced no-code-behind approach to tackle this scenario at this point.

Copyright (c) Marimer LLC