Collection模型类的EditorTemplate命名约定 [英] EditorTemplate Naming Convention for Collection model classes

查看:87
本文介绍了Collection模型类的EditorTemplate命名约定的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个声明为List<MyClass>类型的模型属性.

I have a model property that is declared as type List<MyClass>.

public class MyModel
{
    List<MyClass> MyProperty { get; set; }
}

我希望能够使用Razor模板显示/编辑属性.我的问题是,如何命名EditorTemplate视图,以便可以使用常规语法显示属性:

I want to be able to display/edit the property using Razor templates. My question is, how I name an EditorTemplate view so that I can display the property using the normal syntax:

@model MyModel
@Html.DisplayFor(m => m.MyProperty)

我知道我可以创建一个名为MyClass.cshtml的视图,该视图将用于类型MyClass,但是如何为该列表创建模板?

I know that I can create a view called MyClass.cshtml that will be used for the type MyClass, but how do I create a template for the list?

推荐答案

您可以使用[UIHint]属性:

public class MyModel
{
    [UIHint("TemplateForTheList")]
    public List<MyClass> MyProperty { get; set; }
}

或将模板名称指定为DisplayFor助手的第二个参数:

or specify the template name as second parameter to the DisplayFor helper:

@model MyModel
@Html.DisplayFor(m => m.MyProperty, "TemplateForTheList")

,然后有一个TemplateForTheList.cshtml模板:

@model List<MyClass>
...

在这种情况下,模板引擎将不会为collection属性的每个元素呈现模板.它将简单地将集合本身​​传递给模板.

In this case the templating engine will not render the template for each element of the collection property. It will simply pass the collection itself to the template.

这篇关于Collection模型类的EditorTemplate命名约定的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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