Asp.Net MVC中的ViewModel与Knockout.Js

时间:2022-12-03 07:49:02

I'm really confused about how ViewModel works in the context of KnockOut.js and Asp.Net MVC.

我真的很困惑ViewModel如何在KnockOut.js和Asp.Net MVC的上下文中工作。

In Asp.Net MVC, ViewModels are similar to DTOs. They are used to have strongly typed views. It is considered a good practice, which I've been abinding to as well.

在Asp.Net MVC中,ViewModel与DTO类似。它们习惯于具有强类型视图。这被认为是一种很好的做法,我也一直在努力。

However, recently I got interested in Knockout.js, which uses ViewModels in a whole different way. I'm still not 100% sure, but ViewModels are like Controllers it seems.

然而,最近我对Knockout.js感兴趣,它以完全不同的方式使用ViewModels。我仍然不是百分百肯定,但ViewModel似乎就像控制器一样。

I guess I'm still confused about the ViewModel in knockout.js because its not the same as Asp.Net MVC. Can you please help clarify these two patterns?

我想我仍然对knockout.js中的ViewModel感到困惑,因为它与Asp.Net MVC不同。你能帮忙澄清这两种模式吗?

1 个解决方案

#1


1  

You are correct. The logic in MVC is inside the controller and a view model in this context is basically a strongly-typed ViewBag or DTO for passing data to the view for one-way databinding.

你是对的。 MVC中的逻辑位于控制器内部,此上下文中的视图模型基本上是强类型的ViewBag或DTO,用于将数据传递到视图以进行单向数据绑定。

In Knockout and anything on the XAML stack, a view model is more of a typical MVVM "view model" that is more like a controller that exposes observable properties for two-way databinding.

在Knockout和XAML堆栈上的任何东西中,视图模型更像是一个典型的MVVM“视图模型”,更像是一个暴露双向数据绑定的可观察属性的控制器。

#1


1  

You are correct. The logic in MVC is inside the controller and a view model in this context is basically a strongly-typed ViewBag or DTO for passing data to the view for one-way databinding.

你是对的。 MVC中的逻辑位于控制器内部,此上下文中的视图模型基本上是强类型的ViewBag或DTO,用于将数据传递到视图以进行单向数据绑定。

In Knockout and anything on the XAML stack, a view model is more of a typical MVVM "view model" that is more like a controller that exposes observable properties for two-way databinding.

在Knockout和XAML堆栈上的任何东西中,视图模型更像是一个典型的MVVM“视图模型”,更像是一个暴露双向数据绑定的可观察属性的控制器。