Applying MVVM to ASP.NET

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

Some of the examples I found on the Internet use XAML for presentation.

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

Here is an 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.

+4
source share
8 answers

As other answers point out, MVVM works (mainly) because of the Databinding, which comes free with WPF and Silverlight.

If you are a web developer and want to continue to use your asp.net and html skills, you might want to take a look at KnockoutJS , a javascript library that provides data binding from View to Model or ViewModel.

+6
source

The drawback of the MVVM template is that it only works well when used together with really powerful data binding mechanisms (for example, in WPF). I do not think that MVVM will be very effective when used with ASP.NET WebForms.

+5
source

Is testability the main reason for wanting to use the MVVM design pattern? Assuming this is the case, you will find that MVP is better for WebForms. If you run Google ASP.NET MVP, you'll see some examples. A good book on testing ASP.NET applications in general here is 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 is not trivial, and this is only one of the problems with ASP.NET MVC addresses.

+1
source

You can use MVVM (via knockout) on your MVC View, and the rest will remain MVC. In this case, you will have MVVM on the network, and this is a pleasant solution.

+1
source

MVVM is specifically designed for WPF, where Data Binding takes effect.

check

WPF Applications with Model-View-ViewModel Design Pattern

for more information on MVVM

0
source

One of the keys to using MVVM with webforms is a powerful binding mechanism to support development. Check out the following project for an example of how this can be achieved using web forms.

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

0
source

Try DotVVM .

It is incompatible with Web Forms, but it shares its principles (postbacks, server controls, etc.), it solves their biggest problems (pure HTML, no viewstate, testable viewmodels), and it supports both the full .NET Framework ( via OWIN) and .NET Core.

You do not even need to write any javascript, it uses Knockout JS in the background, the infrastructure solves everything related to client-server exchange for you.

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

The views are as follows:

 <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 viewmodel is a pure C # class.

 public class ContactFormViewModel { public string Name { get; set; } public string Email { get; set; } public void Submit() { ContactService.Submit(Name, Email); } } 
0
source

Source: https://habr.com/ru/post/1333455/


All Articles