使用的ViewData或不使用的ViewData [英] To use ViewData or not to use ViewData

查看:200
本文介绍了使用的ViewData或不使用的ViewData的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在阅读专业ASP.NET MVC 1.0书以及阅读其他来源谈论你的控制器内使用视图模型,而不是从ViewData的意见来。但后来我看到在那些艰难的地方有没有其他办法,只能得到一些ViewData的情景正在使用的ViewData的这样的例子很多。但后来我读了一本书像临ASP.NET MVC框架和所有他谈到只有ViewData的,一无所知视图模型。所以是一个视图模型很新的概念还是什么?

I've been reading the Professional ASP.NET MVC 1.0 book as well as reading other sources talking about using ViewModel instead of ViewData from within your controllers to Views. But then I see so many examples of ViewData being used in scenarios that are tough where there's no other way but to get something from ViewData. But then I read a book like Pro ASP.NET MVC Framework and all he talks about is only ViewData, nothing about ViewModel. So is ViewModel a very new concept or what?

我看到视图模型是一个更好的方法,但它是一个坚实的选择吗?我的意思是的ViewData所以在其他的东西随时提供给您如的HtmlHelper对象,其中视图模型是没有的。或者,例如在自定义控件使用它(<一个href=\"http://www.$c$cproject.com/KB/custom-controls/MVCCustomControls.aspx\">http://www.$c$cproject.com/KB/custom-controls/MVCCustomControls.aspx).所以,做我同时使用这取决于不同的目标还是什么?如果我想访问视图模型在我的扩展方法无论出于何种原因的是什么组合?我在这里失去了为采取什么样的道路。我知道的ViewData不是强类型,但你可以设置您的视图指定类型,从而使你的ViewData的类型,但我不知道,没有为ViewData的这么多的支持,但我知道视图模型是一个更抽象的,分离的路要走,以及它正在键入我只是不希望削减自己短场景中,我将需要抓住某些数据如与其他物体容易获得,如HtmlHelper类的ViewData的。

I see that ViewModel is a much better approach but is it a solid alternative? I mean ViewData is so readily available to you in other things such as the HtmlHelper object where ViewModel is not. Or for example using it in a custom control (http://www.codeproject.com/KB/custom-controls/MVCCustomControls.aspx). So do I use a combo of both depending on different goals or what? What if I want to access the ViewModel in my Extension method for whatever reason? I'm lost here as to what path to take. I know that ViewData is not strongly typed but that you can set your view to specify the type and therefore make your ViewData typed but I just wonder. There is so much support for ViewData but I know ViewModel is a much more abstract and separated way to go as well as it being typed. I just don't want to cut myself short in scenarios where I will need to grab certain data such as the ViewData that is readily accessible from other objects such as the HtmlHelper class.

的思考?标准是什么?经历?我是过了一点还是你只是使用组合并仍然使用的ViewData在其他情况下不是从你的控制器只需将数据发送到您的视图还是什么?

Thoughts? Standards? Experiences? Am I off a bit or do you just use a combo and still use ViewData in other circumstances than just sending data from your Controller to your View or what?

如果你是不是在所有使用的ViewData,而是使用视图模型与你的控制器,这似乎是在一个全有或全无您使用视图模型,因此ViewData的没有目的,因为你还没有与任何设置在你的控制器,因此它在这一点上没有用?我是不是任何人都混淆或关闭此方式?混淆了地狱,自己说的是肯定的。

And If you're not using ViewData at all and instead using ViewModel with your controllers, it seems like an all or nothing in that you are using ViewModel and therefore ViewData has no purpose since you have not set it in with anything from your controllers so it has no use at that point?? Am I confusing anyone or way off here? Confusing the hell out of myself that's for sure.

推荐答案

好了,ViewData的是pretty快速实施方法。但是,你正在做大量的字符串传球这通常不是一件好事。你可以解决,通过使用一些字符串常量,这是我做的Session变量,但我认为这里的ViewModel是一个更好的方法。您可以使用ViewData的任何时候,你也可以使用一个ViewModel。该视图模型并不一定只是你的域对象;这可能是因为已不仅是一个域对象但具体到您的视图一些额外的属性的辅助类;这就是为什么它的存在。于是用视图模型,你让编译器从面向对象的角度帮助您和清晰,这是远远不只是传递密钥字典清洁。

Well, ViewData is a pretty quick to implement method. However, there you're doing a lot of string literal passing which is usually not a good thing. You could solve that by using some string constants, which is what I do with Session variables, but I think here a ViewModel is a much better approach. Any time you can use ViewData, you could also use a ViewModel. The ViewModel doesn't have to be just your domain object; it could be a helper class that has not only a domain object but some extra properties specific to your view; that's why it's there. So with the ViewModel, you have the compiler helping you and clearly from an OO perspective, it is far cleaner than just passing in keys to a dictionary.

我觉得这里的MVC提供了一个很好的办法。它提供了快速和肮脏的(不一定是坏事),为那些需要只是把它做谁和清洁的方法,这两者都是很容易使用。

I think MVC here offers a good approach. It offers and quick and dirty (not necessarily a bad thing) for those who need to "just get it done" and a cleaner approach, both of which are quite easy to use.

如果你还没有读过斯科特Gurthie的ASP.NET MVC教程;我强烈推荐它:

If you haven't read Scott Gurthie's ASP.NET MVC Tutorial; I highly recommend it:

<一个href=\"http://weblogs.asp.net/scottgu/archive/2009/04/28/free-asp-net-mvc-nerddinner-tutorial-now-in-html.aspx\" rel=\"nofollow\">http://weblogs.asp.net/scottgu/archive/2009/04/28/free-asp-net-mvc-nerddinner-tutorial-now-in-html.aspx

这篇关于使用的ViewData或不使用的ViewData的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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