是否可以在ASP.NET MVC项目中利用ICommand? [英] Is it possible to take advantage of the ICommand in an ASP.NET MVC project?

查看:85
本文介绍了是否可以在ASP.NET MVC项目中利用ICommand?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

基本上,我正在创建一个可移植的类库,并创建了一堆ViewModel:

Basically, I am creating a portable class library, and creating 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) { ... }
}

如果我想要WinRT和WPF应用程序的可重用性,那就太好了,但是如何为ASP.NET MVC网站利用此类呢?似乎我无论如何都必须将此类所做的所有操作包装到Controller中.另外,ICommand是否甚至可用于ASP.NET MVC应用程序?

This is great if I want for reusability for WinRT and WPF apps, but how can I take advantage of this class for an ASP.NET MVC website? It seems as if I have to just wrap everything this class does into a Controller anyways. Also, is ICommand even usable for an ASP.NET MVC app?

我真的不想拥有一个客户端MVVM javascript库,它只是我的视图模型的重新编码版本.

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

推荐答案

在MVC ViewModels中直接使用此功能至少毫无意义且功能不正常.这是因为与WPF中的MVVM不同,ViewModel的实例不是跨交互(单击,POST等)保留的,而是重新创建的.因此,整个INPC(INotifyPropertyChanged)将无法正常工作.

Direct use of this in the MVC ViewModels would be at least pointless and dysfunctional. This is because unlike MVVM in WPF the instance of the ViewModel is not kept across interactions (clicks, POSTs etc.) but re-created. Therefore the whole INPC (INotifyPropertyChanged) would not work.

需要基于视图将数据绑定到的其他属性来启用/禁用ICommand,但是MVC中的数据绑定(可以这么说)是一次性的(在渲染时),这也将使该概念无用.

ICommand would need to be enabled/disabled based on other properties to which the view would be data bound but the databinding (so to speak) in MVC is one-time (at render) which would render this concept useless, also.

MVVM和MVC不兼容,很抱歉.几年前,我曾尝试过在同一平台上重复使用相同的问题,但我做不到.甚至没有T4(文本模板),我试图将(WPF风格的)ViewModel类转换为MVC更加可接受的形式.

MVVM and MVC are not compatible, sorry. I went through the same problem a few years ago trying to reuse stuff across platforms but I couldn't. Not even with T4 (text templates) which I tried to transform the (WPF-ish) ViewModel class into a more acceptable form for MVC.

这篇关于是否可以在ASP.NET MVC项目中利用ICommand?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆