Master/Detail in separate viewmodels?

I am designing a master/detail view. Currently, I have a user control (detail) in my main view, and both have thier own vm. On the one hand, I think there should only be one vm because the detail will never exist without the master. It would also be easier to handle the CRUD process in one vm because of their tight dependency on one another. On the other hand, they are separate entities, and having two smaller vm vs one large one seems more manageable. Any thoughts?

Answers


For the given scenario I would've created two different views and bound it to the same viewmodel.

If you lazy load stuff from a database, it might be cleaner to implement two viewmodels .


The view model is the model of the view. If you have two views, each has a view model. If the views are interdependent, the models will be too.

The wisdom of having a separate view model for detail items becomes apparent as the complexity of your detail items grows. For a simple example, imagine a hyperlink presenting a command in the detail view, which should be enabled if the detail item meets some kind of criteria. Where are you going to put the source of the hyperlink's command binding?


One of my apps has this scenario. I have a master ListView with items and a detail view with extended information about the selected one. Detail view has its own view model.

I binded the detail view DataContext with the SelectedItem from the master by using a Converter:

<view:MyDetailView Grid.Row="2"
                   DataContext="{Binding Path=SelectedItem, ElementName=masterList, Converter={StaticResource EntityToDetailViewModelConverter}}">            
</view:FontDetailView>

And the converter,

class EntityToDetailViewModelConverter: System.Windows.Data.IValueConverter
{
    public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
    {
        MyEntity entity = value as MyEntity;
        return new ViewModel.MyDetailViewModel(entity);
    }

    public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
    {
        throw new NotSupportedException();
    }
}

Need Your Help

jquery mobile hide the main content after submit a form with ajax

javascript jquery ajax mobile

When I submit the form with ajax everything seems to work but the content is hidden. I can only see it on the "inspect element" but not in the browser.

Teamcity SSH private key login failed: invalid privatekey

ssh teamcity jsch teamcity-8.0

I set SSH login which connect from Windows Agent to Linux but TeamCity gives following error.