asp.net MVC 中的 ViewData.* 和 TModel
使用了一周的asp.net mvc2后,我仍然没有理解ViewData.model的优点,或者更确切地说,我如何正确地利用Viewdata。有人可以教我如何正确使用Viewdata
吗?
另外,与 viewdata 关联的 TModel
是什么?如何利用TModel
? spark view引擎中的viewdata解释谈论了TModel
,但我不能了解如何在我的项目中使用它。有人可以帮助我吗?
After a week of asp.net mvc2, I still haven’t understood the advantages of ViewData.model
or rather how I can properly utilize Viewdata. Can some teach me how to use Viewdata
properly?
Also what’s TModel
that’s associated with viewdata? How does one utilize TModel
? The viewdata explanation in spark view engine talks about TModel
and I couldn’t get a clue of how I can use it in my projects. Can someone help me?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
ViewData.Model 是您可以在控制器操作中设置并传递到视图的东西,您可以在其中像这样
或
那样访问它,如果您传递到视图的类包含属性描述:
使用此 MyContent 类
基本上您是将类的实例(设置了属性的对象,很可能从数据库中获取)发送回视图并在视图中显示其数据,视图是 ascx 或 aspx 文件,最终显示给用户/访问者。这是一个非常简单的例子,但不清楚你到底想要什么以及你已经知道多少。但在您充分了解 ASP.NET MVC 基础知识之前,请暂时不要考虑 Spark(和其他视图引擎)。
ViewData.Model is something that you can set in controller action and gets passed to the View where you can access it like this
or
that is, if the class that you are passing to the View contains property Description:
with this MyContent class
Basically you are sending an instance of a class (an object with its properties set, most likely taken from the database) back to the View and display its data in the View, View being the ascx or aspx file, that eventually gets display to the user/visitor. This is very simple example but it is unclear what exactly you want and how much you already know. But try to leave Spark (and other View Engines) out of the question for now until you know the ASP.NET MVC basics well.
Mare 是正确的,您可以通过访问 ViewData.ModelName.PropertyName 项在视图中使用模型。
另外,在控制器中,您可以在 ViewData 字典中设置某些键/值对:
然后在视图中访问它:
显然,使用键/值对来处理所有数据并不理想,这就是为什么您可以创建您自己的类来封装数据,并将其传递到您的视图以便于操作。
Mare is correct, you can use your models in your view by accessing the ViewData.ModelName.PropertyName item.
Also while in your controller you can set certain key/value pairs in the ViewData dictionary:
And then access it in your view:
Obviously it wouldn't be ideal to use a key/value pair to handle all of your data, thats why you can create your own classes to encapsulate data, and pass THOSE to your view for easier manipulation.