ASP.NET MVC 4编辑模板基本类型 [英] ASP.NET MVC 4 Editor Template for basic types
问题描述
我已经为数字输入模板,如果我做
I've created a template for Number input and if I do
@Html.EditorFor(model => model.SomeValue, "Number")
它工作正常,并且所使用的模板。
it works fine and the template is used.
然而,这并不工作:
However that doesn't work:
@Html.EditorFor(model => model.SomeValue)
为什么我需要为基本类型指定编辑器模板的名称?
Why do I need to specify the name of the Editor Template for basic types ?
推荐答案
按照惯例编辑器模板的工作。模板的名称必须与类型的名称相匹配。因此,例如,如果 someValue中
是 INT
键入你可以写在自定义编辑器模板〜 /Views/Shared/EditorTemplates/Int32.cshtml
这将被使用。在这种情况下,当你写的所有的整数类型将使用这个自定义模板 @ Html.EditorFor(型号=> model.SomeValue)。
Editor templates work by convention. The name of the template must match the name of the type. So for example if SomeValue
is an int
type you could write a custom editor template at ~/Views/Shared/EditorTemplates/Int32.cshtml
which will be used. In this case all integer types will use this custom template when you write @Html.EditorFor(model => model.SomeValue)
.
如果您不希望覆盖的整数类型你可以写一个特定的命名模板〜/查看/共享/ EditorTemplates / Number.cshtml
所有模板,你可以仅通过指定该模板的名称作为第二个参数 EditorFor
辅助使用一些属性: @ Html.EditorFor(型号=> model.SomeValue, 数)
或与 [UIHint]
属性装饰你的视图模型属性:
If you don't want to override all templates for integer types you could write a specific named template ~/Views/Shared/EditorTemplates/Number.cshtml
that you could use only for some properties by specifying this template name as second argument to the EditorFor
helper: @Html.EditorFor(model => model.SomeValue, "Number")
or by decorating your view model property with the [UIHint]
attribute:
[UIHint("Number")]
public int SomeValue { get; set; }
,然后简单地使用 @ Html.EditorFor(型号=> model.SomeValue)
将呈现 Number.cshtml
自定义模板。
我也建议你阅读布拉德·威尔逊的<一个href=\"http://bradwilson.typepad.com/blog/2009/10/aspnet-mvc-2-templates-part-3-default-templates.html\"><$c$c>blog后 关于ASP.NET MVC的默认模板。
I would also recommend you reading Brad Wilson's blog post
about the default templates in ASP.NET MVC.
这篇关于ASP.NET MVC 4编辑模板基本类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!