C# · 12月 22, 2021

c# – MVC ASP.NET中的ViewData和ViewModel

我是新来的.Net开发,现在正在遵循NerdDinner教程.只是想知道你们中有谁能够告诉我

What is the differences between ViewData
and viewmodel

(所有我知道他们习惯于从控制器传递一些形式的数据来查看),也许告诉我应该使用ViewData而不是viewmodel的情况,反之亦然

提前致谢!

出击

@H_404_13@解决方法 ViewData的:

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

视图模型:

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.

@H_404_13@ @H_404_13@