EditorTemplate Naming Convention for Collection model classes

ε祈祈猫儿з 提交于 2019-12-31 02:58:11

问题


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

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

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)

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?


回答1:


You could use the [UIHint] attribute:

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

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

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

and then have a TemplateForTheList.cshtml template:

@model List<MyClass>
...

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.



来源:https://stackoverflow.com/questions/16941013/editortemplate-naming-convention-for-collection-model-classes

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!