且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

是否可以在ASP.NET MVC项目中利用ICommand?

更新时间:2023-02-15 23:21:08

在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.