开发者

MVVM pattern for ASP.NET Webforms?

开发者 https://www.devze.com 2022-12-09 00:55 出处:网络
I\'m looking for an MVVM implementation for ASP.NET. How should I approach this ? Can you propose any design that solves this problem particularly for ASP.NET Webfo开发者_运维知识库rms ?

I'm looking for an MVVM implementation for ASP.NET. How should I approach this ? Can you propose any design that solves this problem particularly for ASP.NET Webfo开发者_运维知识库rms ?

Thanks.


Take a look at ASP.NET MVVM on CodePlex.

The framework author also talks about it a little in Presentation Model in Action.


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 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);
    }
}


A lot of MVC'ers are doing something akin to a view model in the sense that instead of returning domain objects to the controller, they have a flattened data structure (a view model) of all the data needed for that view regardless of how many domain objects worth of data it contains. In that regard a view model is very doable with MVC, and I'm sure it could be leveraged in webforms as well. However, there is no way that I know of to do the two way databinding / commanding / event aggregation that is associated with MVVM in WPF.

Although I don't know of any webform implimentations you could try some of the approaches described here:
Jimmy Bogard - How we do MVC

Here is a very interesting article on how to do MVP in winforms:
Castle Windsor's MVP with ASP.NET

Maybe you can create a hybrid of these two approaches using webforms.


Have a look at a demo of the ViewModel concept done simply and clearly by Craig Shoemaker. He blogged about it here and the code sample is available too.


https://github.com/Zhestokus/dotNet-ASP.NET.TwoWayModel little library, provides functionality for pattern like MVC/MVVM without third party JavaScript frameworks

e.g you can bind some control property to class property

<asp:TextBox runat="server" ID="tbxEmail" Property="{SubscriberModel.Email=Text}" />
0

精彩评论

暂无评论...
验证码 换一张
取 消