ViewData和ViewModel在MVC ASP.NET [英] ViewData and ViewModel in MVC ASP.NET

查看:336
本文介绍了ViewData和ViewModel在MVC ASP.NET的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是.Net开发的新手,现在是NerdDinner教程。只要想知道你有没有人能够告诉我


ViewData
和ViewModel之间的区别是什么?


(我知道他们是用来从控制器传递一些形式的数据到视图),也许告诉我应该使用ViewData的情况




$ b


$ b class =h2_lin>解决方案

ViewData:



简而言之,使用ViewData作为支持数据,例如

ViewModel:



ASP.NET MVC ViewModel模式


当Controller类决定将HTML响应呈现给
客户端时,它负责
显式传递给视图
模板$ b需要的所有数据$ b呈现响应。查看模板
不应该执行任何数据
检索或应用程序逻辑 - 和
应该限制自己到
只有渲染代码
驱动模型/



当使用[...]时, ViewModel]模式,我们创建强类型的
类,这些类针对我们的
特定视图场景进行了优化,其中
暴露了我们的动态
值/内容的属性查看
模板。我们的控制器类可以
然后填充和传递这些
视图优化的类到我们的视图
模板使用。这使
类型安全,编译时检查,
和编辑器intellisense在视图
模板中。



I'm new to .Net development, and now are following NerdDinner tutorial. Just wondering if any of you would be able to tell me

What is the differences between ViewData and ViewModel

(all I know is they are used to pass some form of data from controller to view) and perhaps tell me on what situation should I use ViewData instead of ViewModel and vice versa

Thanks in advance!

Sally

解决方案

ViewData:

In short, use ViewData as support data, such as a datasource to a SelectList.

ViewModel:

ASP.NET MVC ViewModel Pattern

When a Controller class decides to render an HTML response back to a client, it is responsible for explicitly passing to the view template all of the data needed to render the response. View templates should never perform any data retrieval or application logic – and should instead limit themselves to only have rendering code that is driven off of the model/data passed to it by the controller.

[...]

When using [the "ViewModel"] pattern we create strongly-typed classes that are optimized for our specific view scenarios, and which expose properties for the dynamic values/content needed by our view templates. Our controller classes can then populate and pass these view-optimized classes to our view template to use. This enables type-safety, compile-time checking, and editor intellisense within view templates.

这篇关于ViewData和ViewModel在MVC ASP.NET的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆