简体   繁体   English

MVC自定义显示模板:Model.Value与@ ViewData.Model

[英]MVC Customizing display template: Model.Value vs @ViewData.Model

I've seen two "Display templates customezing" examples. 我看过两个“自定义显示模板”示例。

In the 1. example programmer use Model.Value and in the 2. example he use @ViewData.Model.- See below 在1.示例中,程序员使用Model.Value;在2.示例中,他使用@ ViewData.Model。

Exampel 1 (DateTime view:) 示例1(DateTime视图:)

@model DateTime?
@Html.TextBox("", Model.HasValue ? Model.Value.ToString("dd/MM/yyyy") : "", new { @class = "ka_" })

Example 2: 范例2:

<a href="@ViewData.Model" target="_blank">@ViewData.Model<a/>

What is the difference between them ? 它们之间有什么区别?

What to select ? 选择什么?

In most cases you use both in your views. 在大多数情况下,您会在视图中同时使用两者。 The model specifies the data for you're main view. 该模型为您的主视图指定数据。 It's the data that is created by the controller action. 这是由控制器操作创建的数据。 The ViewData dictionary is normally populated by other components. ViewData词典通常由其他组件填充。

Let's take for example an Product (view)model. 让我们以产品(视图)模型为例。 A product would have a price, a description, a sku and in most cases a stock indication. 产品将具有价格,说明,SKU以及大多数情况下的库存指示。

You're Product (view)model does not contain data any user information, website navigational data. 您的产品(视图)模型不包含任何用户信息,网站导航数据。 That data is placed in the ViewData dictionary by other components and can be used by (parent) views to render the full page. 该数据由其他组件放置在ViewData词典中,并且(父)视图可以使用它们来呈现整个页面。

A bit offtopic, but at our company we have a very strict rules about what a view is allowed to do. 有点离题,但在我们公司,对于允许视图执行的操作我们有非常严格的规则。 In short is it not allowed to change/modify any data (not even the page title!) or to retrieve any data. 简而言之,不允许更改/修改任何数据(甚至不包括页面标题!)或检索任何数据。 The only code allowed in our views are conditional constructs and calls to helper methods. 我们视图中唯一允许的代码是条件构造和对辅助方法的调用。 It helps if new developers on the team start with view engines that have no an 'eval' constructs like SuperSimpleViewEngine (from NancyFx) or XsltViewEngine. 如果团队中的新开发人员从没有像SuperSimpleViewEngine(来自NancyFx)或XsltViewEngine这样的“评估”结构的视图引擎开始,这将有所帮助。 Razor and WebForms make it to easy to 'cheat'.. Razor和WebForms使其易于“欺骗”。

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

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