gpt4 book ai didi

asp.net-mvc-3 - 如何在 Razor 中为 Nullable 创建编辑器模板

转载 作者:行者123 更新时间:2023-12-04 07:40:02 25 4
gpt4 key购买 nike

一直试图找到使用 razor 为可空类型创建编辑器模板的方法。我在 LinqToSQL 类中有 Guid 和 Nullable 类型的属性。当我使用 @model Guid?在文件的第一行 Guid.cshtml ,它总是假设 Guids 作为 Guid?类型。我想为 Guid? 创建一个不同的编辑器模板,但文件名不接受 ?或 < 或 > 字符。

请帮忙。谢谢。

最佳答案

似乎对于可为空值类型的意图是编写一个显示/编辑器模板来处理可为空和不可为空类型。这是 post 的摘录通过布拉德威尔逊:

When searching for the type name, the simple name is used (i.e., Type.Name) without namespace. Also, if the type is Nullable, we search for T (so you’ll get the Boolean template whether you’re using “bool” or “Nullable”). This means if you’re writing templates for value types, you will need to account for whether the value is nullable or not. You can use the IsNullableValueType property of ModelMetadata to determine if the value is nullable. We’ll see an example of this below with the built-in Boolean template.



所以,你会使用 @model Guid?在您的模板中并检查空值。

关于asp.net-mvc-3 - 如何在 Razor 中为 Nullable<T> 创建编辑器模板,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4551909/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com