的ViewData和视图模型在ASP.NET MVC [英] ViewData and ViewModel in MVC ASP.NET

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

问题描述

我是新来的.Net开发,现在跟随的NerdDinner教程。只是想知道如果你们任何人将能够告诉我


  

什么是ViewData的之间的差异
  和视图模型


(我只知道他们是用来传递某种形式的数据,从控制器查看),或许告诉我在什么情况下我应该使用,而不是视图模型和ViewData的反之亦然

在此先感谢!

萨利


解决方案

ViewData的:

在短,使用的ViewData作为支持数据,如数据源到的SelectList

视图模型:

ASP.NET MVC视图模型模式


 当一个控制器类决定渲染HTML响应回


  
  

客户端,它负责
  明确地传递给视图
  模板所需的所有数据的
  呈现的响应。视图模板
  不应该执行任何数据
  检索或应用程序逻辑 - 与
  而应该把自己限制
  只有已经呈现code是
  驱动关闭模型/传递到数据
  它由控制器

  [...]当使用[在视图模型]模式,我们建立强类型

这是优化我们的班
  具体看场景,并
  暴露的动态属性
  我们认为需要值/内容
  模板。我们的控制器类可以
  然后填充和传递这些
  鉴于优化类我们的观点
  要使用的模板。这使
  类型安全,编译时检查,
  观内和编辑智能感知
  模板。


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和视图模型在ASP.NET MVC的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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