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

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

问题描述

我是 .Net 开发的新手,现在正在关注 NerdDinner 教程.只是想知道你们中是否有人能告诉我

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

ViewData 有什么区别和视图模型

What is the differences between ViewData and ViewModel

(我所知道的是它们用于将某种形式的数据从控制器传递到视图)并且可能告诉我在什么情况下我应该使用 ViewData 而不是 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

提前致谢!

莎莉

推荐答案

ViewData:

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

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

视图模型:

ASP.NET MVC ViewModel 模式

当控制器类决定将 HTML 响应呈现回客户,它负责显式传递给视图模板化所有需要的数据呈现响应.查看模板永远不应该执行任何数据检索或应用逻辑——以及应该把自己限制在只有渲染代码是从传递给的模型/数据驱动由控制器控制.

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.

[...]

当使用 [the "ViewModel"] 模式时,我们创建了强类型为我们优化的类具体的视图场景,以及公开动态的属性我们的观点所需的价值观/内容模板.我们的控制器类可以然后填充并传递这些视图优化类到我们的视图要使用的模板.这使类型安全,编译时检查,和编辑器智能感知模板.

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.

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

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