问题描述
虽然我在读这本书ASP.NET MVC 4我想了解一下MVVM。我开始google搜索,并不能找到有关使用MVVM开发Web应用程序的任何书籍,所以我必须在这里失去了一点信息。
While I'm reading the book "ASP.NET MVC 4" I'm wondering about MVVM. I started googling and cannot find any books about developing web applications using MVVM, so I must be missing a bit of information here.
据我了解,MVVM是在客户端Web应用程序通过knockout.js和其他框架中使用。
但是,如果我是开发一个Windows Phone应用程序,我可以直接使用MVVM,而无需使用MVC。
这是否意味着,MVVM /数据只是绑定的概念并不适用于客户端 - 服务器的Web应用程序?
From what I understand, MVVM is used in web applications on the client side via knockout.js and other frameworks.If however I was to develop a Windows Phone application, I could use MVVM directly without using MVC.Does that mean, the concept of MVVM / data binding just does not apply to client-server web applications?
推荐答案
MVVM真是形式的子模式。有没有真正的MVVMWeb应用程序框架存在。他们都MVC和你pretty多,如果你想要一个只包含一个视图模型。
MVVM is really sort of a subpattern. There's not really any "MVVM" web app frameworks out there. They're all MVC and you pretty much just incorporate a view model if you want one.
使用ASP.NET MVC,特别是你刚才创建一个类,一般在 [产品型号]视图模型
或 [产品型号] VM 。这个类将只有从你的模型,你需要有和任何额外的没有意义,把你的实际的数据库支持的模型,如的SelectList $ C $工作性质C> S,等等。
With ASP.NET MVC, in particular, you just create a class, generally with a name in the form of [Model Name]ViewModel
or [Model Name]VM
. That class will have only the properties from your model that you'll need to work with and anything extra that doesn't make sense to put on your actual database-backed model, like SelectList
s, etc.
在你的行动,你只是通过这个视图模型的实例来你的视图,而不是模型的:
In your action, you just pass an instance of this view model to your view instead of your model:
return View(viewModelInstance);
和,当然,请确保您的看法接受:
And, of course, make sure your view accepts that:
@model Namespace.To.MyViewModel
唯一稍微复杂的部分是布线视图模型到模型(即从视图模型/模型获取数据/你可以手动通过显式映射属性做到这一点,或者你可以使用类似的。
这篇关于并在MVVM ASP.NET MVC 4 Web应用程序中发挥什么作用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!