我希望能为字符串属性创建一个显示模板,并对其他所有属性使用默认值。
我尝试在 Views/Shared/DisplayTemplates 中创建一个 string.cshtml 文件,其中包含以下内容:
我现在遇到了一个问题,当我尝试打开任何使用DisplayFor(m => m.property)的视图时,会显示错误,例如: The model item passed into the dictionary is of type 'System.DateTime', but this dictionary requires a model item of type 'System.String'. 或者: The model item passed into the dictionary is of type 'System.Int64', but this dictionary requires a model item of type 'System.String'。
我知道可以通过为每个使用的类型添加显示模板来解决这个问题,但我认为在未定义自定义模板的所有类型中使用“默认”模板也是可能的吗?
更新: 在Darin的回答后,我检查了Brad的教程并将模板更改为:
这是基于“默认”模板的,适用于所有类型。
我尝试在 Views/Shared/DisplayTemplates 中创建一个 string.cshtml 文件,其中包含以下内容:
@model string
@Html.TextBoxFor(m => m, new { @readonly = "readonly" })
我现在遇到了一个问题,当我尝试打开任何使用DisplayFor(m => m.property)的视图时,会显示错误,例如: The model item passed into the dictionary is of type 'System.DateTime', but this dictionary requires a model item of type 'System.String'. 或者: The model item passed into the dictionary is of type 'System.Int64', but this dictionary requires a model item of type 'System.String'。
我知道可以通过为每个使用的类型添加显示模板来解决这个问题,但我认为在未定义自定义模板的所有类型中使用“默认”模板也是可能的吗?
更新: 在Darin的回答后,我检查了Brad的教程并将模板更改为:
@Html.TextBox("", ViewData.TemplateInfo.FormattedModelValue, new { @readonly = "readonly" })
这是基于“默认”模板的,适用于所有类型。