Can ICommand be used in ASP.NET MVC project?

Basically, I create a portable class library and create a bunch of ViewModels:

public class CustomerViewModel : ViewModelBase
{
    public string FirstName { ... } // INPC
    public string LastName { ... } // INPC

    public string FullName
    {
        get { return FirstName + " " + LastName; }
    }

    public ICommand DoFooCommand { get; private set; } // DelegateCommand

    private bool CanDoFoo(object parameter) { ... }
    private void DoFoo(object parameter) { ... }
}

      

It's great if I want to reuse it for WinRT and WPF applications, but how can I take advantage of this class for an ASP.NET MVC website? It seems I just need to wrap everything this class does in a controller. Also, is ICommand even suitable for ASP.NET MVC application?

I really don't want to have a JavaScript MVVM client library that is just a recoded version of my view models.

+3


source to share


1 answer


Using this directly in MVC ViewModels would be at least pointless and dysfunctional. This is because unlike MVVM in WPF, the ViewModel instance is not supported across all interactions (clicks, POST, etc.), but is recreated. So the whole INPC (INotifyPropertyChanged) won't work.

ICommand will need to be enabled / disabled based on other properties that the view will be data bound to, but data binding (so to speak) in MVC is one-off (when rendered), which would also make this concept useless.



MVVM and MVC are not compatible, sorry. Several years ago I ran into the same problem trying to reuse stuff across different platforms, but I couldn't. Even with T4 (text templates) I was trying to convert the ViewModel class (WPF-ish) into a more MVC-friendly form.

0


source







All Articles