gpt4 book ai didi

asp.net-mvc-4 - 基本类型的 ASP.NET MVC 4 编辑器模板

转载 作者:行者123 更新时间:2023-12-03 06:33:50 24 4
gpt4 key购买 nike

我已经创建了一个用于数字输入的模板,如果我这样做的话

@Html.EditorFor(model => model.SomeValue, "Number")

它工作正常并且使用了模板。

但是这不起作用:

@Html.EditorFor(model => model.SomeValue)

为什么需要为基本类型指定编辑器模板的名称?

最佳答案

编辑器模板按惯例工作。模板的名称必须与类型的名称匹配。例如,如果 SomeValueint 类型,您可以在 ~/Views/Shared/EditorTemplates/Int32.cshtml 编写自定义编辑器模板,其中将会被使用。在这种情况下,当您编写 @Html.EditorFor(model => model.SomeValue) 时,所有整数类型都将使用此自定义模板。

如果您不想覆盖整数类型的所有模板,您可以编写一个特定的命名模板 ~/Views/Shared/EditorTemplates/Number.cshtml ,您只能将其用于某些属性将此模板名称指定为 EditorFor 帮助器的第二个参数:@Html.EditorFor(model => model.SomeValue, "Number") 或通过使用以下命令装饰 View 模型属性[UIHint] 属性:

[UIHint("Number")]
public int SomeValue { get; set; }

然后只需使用 @Html.EditorFor(model => model.SomeValue) 将呈现 Number.cshtml 自定义模板。

我还建议您阅读 Brad Wilson 的 blog post关于 ASP.NET MVC 中的默认模板。

关于asp.net-mvc-4 - 基本类型的 ASP.NET MVC 4 编辑器模板,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14655930/

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