将MVVM应用于ASP.NET

4 asp.net mvvm

我正在学习MVVM,我想在ASP.NET中使用它.

我在互联网上找到的一些例子使用XAML作为视图.

有没有办法为视图使用常规ASP.NET页面而不是XAML?

这是一个XAML示例:

<UserControl x:Class="MVVMExample.DetailView"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
    <Grid x:Name="LayoutRoot" Background="White" 
                 DataContext="{Binding CurrentContact}">
        <Grid.RowDefinitions>
            <RowDefinition/>
            <RowDefinition/>
        </Grid.RowDefinitions>
        <Grid.ColumnDefinitions>
            <ColumnDefinition/>
            <ColumnDefinition/>
        </Grid.ColumnDefinitions>
        <TextBlock Text="Name:" HorizontalAlignment="Right" Margin="5"/>
        <TextBlock Text="{Binding FullName}" 
               HorizontalAlignment="Left" Margin="5" Grid.Column="1"/>
        <TextBlock Text="Phone:" HorizontalAlignment="Right" 
               Margin="5" Grid.Row="1"/>
        <TextBlock Text="{Binding PhoneNumber}" 
               HorizontalAlignment="Left" Margin="5" 
               Grid.Row="1" Grid.Column="1"/>
    </Grid> </UserControl>
Run Code Online (Sandbox Code Playgroud)

感谢您的时间.

kiw*_*pom 6

正如其他答案所指出的那样,MVVM(主要)是由于数据绑定而工作的,它通过WPF和Silverlight免费提供.

如果您是一名Web开发人员,并希望继续使用您的asp.net和html技能,那么看看KnockoutJS可能是值得的- 一个javascript库提供从View到Model或ViewModel的数据绑定.


Tom*_*mas 5

MVVM模式的缺点是,它与真正强大的数据绑定机制(如WPF中的机制)一起使用时才能正常工作 - 我不认为MVVM在与ASP.NET WebForms一起使用时会非常有效.