用于基本类型的 ASP.NET MVC 4 编辑器模板 [英] ASP.NET MVC 4 Editor Template for basic types

查看:20
本文介绍了用于基本类型的 ASP.NET MVC 4 编辑器模板的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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.

但是这不起作用:

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

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

Why do I need to specify the name of the Editor Template for basic types ?

推荐答案

编辑器模板按惯例工作.模板名称必须与类型名称匹配.例如,如果 SomeValueint 类型,您可以在 ~/Views/Shared/EditorTemplates/Int32.cshtml 编写自定义编辑器模板,其中将会被使用.在这种情况下,当您编写 @Html.EditorFor(model => 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).

如果您不想覆盖整数类型的所有模板,您可以编写一个特定的命名模板 ~/Views/Shared/EditorTemplates/Number.cshtml,您只能将其用于某些属性将此模板名称指定为 EditorFor 助手的第二个参数:@Html.EditorFor(model => model.SomeValue, "Number") 或通过装饰您的视图模型属性使用 [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 => model.SomeValue) 将呈现 Number.cshtml 自定义模板.

and then simply using @Html.EditorFor(model => model.SomeValue) will render the Number.cshtml custom template.

我还建议您阅读 Brad Wilson 的 blog post 关于 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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆