4

I am learning MVVM and i want to use it with ASP.NET.

Some of the examples that i found on the internet uses XAML for the view.

Is there a way to use a regular ASP.NET page instead of XAML for the view?

Here is a XAML example:

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

Thank you for your time.

8 Answers8

6

As the other answers point out, MVVM works (primarily) due to Databinding, which comes for free with WPF and Silverlight.

If you are a web dev, and want to continue using your asp.net and html skills, it might be worth having a look at KnockoutJS - a javascript library that provides databinding from View to Model or ViewModel.

kiwipom
  • 7,639
  • 37
  • 37
  • You may want to checkout the following which enables some level of server side data binding using a WPF inspired syntax: https://github.com/samshiles/MVVM-4-Webforms – Sam Shiles Dec 28 '13 at 16:08
5

The downside of MVVM pattern is that it only works well when used together with really powerful databinding mechanisms (like the ones in WPF) - I don't think MVVM will be very effective when used with ASP.NET WebForms.

Tomas
  • 553
  • 4
  • 9
1

You can use MVVM (with the help of knockout) on your MVC's View and the rest would remain MVC. In this case you will have your MVVM in web and it's a nice solution.

1

Is testability the primary reason for wanting to use the MVVM design pattern? Assuming it is, then you'll find MVP a better fit for WebForms. If you google ASP.NET MVP you'll see some examples. A good book on testing ASP.NET applications in general is here - http://www.wrox.com/WileyCDA/WroxTitle/Testing-ASP-NET-Web-Applications.productCd-0470496649,descCd-description.html.

Alternatively, if this is a new project, then I would seriously consider ASP.NET MVC, because testability in WebForms isn't trivial, and that's just one of the problems ASP.NET MVC addresses.

devdigital
  • 34,151
  • 9
  • 98
  • 120
  • Could you please answer http://stackoverflow.com/questions/8851933/event-bubbling-and-mvp-asp-net ? – LCJ Jan 18 '12 at 05:23
0

One of the keys to using MVVM with webforms is getting a powerful binding mechanism to aid development. Check out the following project for an example of how this might be achieved with webforms.

https://github.com/samshiles/MVVM-4-Webforms

Sam Shiles
  • 10,529
  • 9
  • 60
  • 72
0

Try DotVVM.

It is not compatible with Web Forms, but it shares its principles (postbacks, server controls etc.), it solves their biggest issues (clean HTML, no viewstate, testable viewmodels) and it supports both full .NET Framework (through OWIN) and .NET Core.

You don't even need to write any javascript, it uses Knockout JS on the background, the framework solves everything concerning client-server communication for you.

It has also a nice Visual Studio 2015 integration and it is open source.

The views look like this:

<div class="form-control">
    <dot:TextBox Text="{value: Name}" />
</div>
<div class="form-control">
    <dot:TextBox Text="{value: Email}" />
</div>
<div class="button-bar">
    <dot:Button Text="Submit"
        Click="{command: Submit()}" />
</div>

And the viewmodel is pure C# class.

public class ContactFormViewModel 
{
    public string Name { get; set; }
    public string Email { get; set; }

    public void Submit() 
    {
        ContactService.Submit(Name, Email);
    }
}
Tomáš Herceg
  • 1,595
  • 1
  • 13
  • 18
0

MVVM is specially designed for WPF where Data Binding comes into action.

check

WPF Apps With The Model-View-ViewModel Design Pattern

for detailed information about MVVM

Binil
  • 6,445
  • 3
  • 30
  • 40
0

ASP.NET MVVM for Web Form

test5
  • 3
  • 1
  • 3
    Welcome to Stack Overflow! While this may theoretically answer the question, [it would be preferable](http://meta.stackexchange.com/q/8259) to include the essential parts of the answer here, and provide the link for reference. – Michael Myers Feb 29 '12 at 23:37