简体   繁体   English

什么是模特? 为什么我们需要使用它

[英]Whats in a model? Why we need to use it

Have one Doubt In MVC Architecture we can able to pass data from Controller to Directly view without creating strongly typed view, than why there is need to use Models ..? 有一个疑问在MVC架构中我们能够将数据从Controller传递到Directly视图而不创建强类型视图,而不是需要使用模型的原因..?

Is it decreases the Performance of the application if we are saving data in ViewData[""] and use it in Views to display Information . 如果我们在ViewData [“”]中保存数据并在Views中使用它来显示信息,它会降低应用程序的性能吗? . ?

Storing data in a collection (ViewData) technically will be ever so slightly slower than passing in a strongly typed model (concrete class), but the difference is extremely small. 从技术上讲,将数据存储在集合(ViewData)中的速度要比传入强类型模型(具体类)慢得多,但差别非常小。 There is also a tiny increase to the memory footprint (because you need memory for the collection and for the thing(s) in the collection) but again, that should be inconsequential. 内存占用量也有微小的增加(因为你需要内存用于集合和集合中的东西),但同样,这应该是无关紧要的。

Strongly typed models provide a clear contract between the controller and the view. 强类型模型在控制器和视图之间提供了明确的契约。 I prefer them in all cases personally. 在所有情况下,我个人更喜欢它们。

There can be simple views where the developer feels it unnecessary to create a strong type to represent the controller/view contract. 可以有简单的视图,开发人员认为没有必要创建一个强类型来表示控制器/视图合同。 For that need, ViewData exists. 为此需要,ViewData存在。

I imagine that ViewData is also used by some to pass extra data not originally envisioned in the strongly typed model. 我想某些人也使用ViewData来传递强类型模型中最初未设想的额外数据。 I would encourage refactoring the strong type in such cases rather than passing extra data in ViewData. 我鼓励在这种情况下重构强类型,而不是在ViewData中传递额外的数据。

There may be "legitimate" uses of ViewData that I'm missing, but I have not come across any thus far. 我可能缺少ViewData的“合法”用法,但到目前为止我还没有遇到过。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM