Creating an object.cshtml editor template with MVC and Razor

后端 未结 2 1509
不知归路
不知归路 2021-02-20 00:46

I am looking to create an editor template for Object.cshtml to change the behavior of the Html.EditorForModel() method. I can\'t find any example of this using Razor. I have see

2条回答
  •  醉酒成梦
    2021-02-20 01:18

    I'm just going to do the Display template and leave the rest as an exercise for the reader:)

    @if (Model == null) {
        @ViewData.ModelMetadata.NullDisplayText
    } else if (ViewData.TemplateInfo.TemplateDepth > 1) {
        @ViewData.ModelMetadata.SimpleDisplayText
    } else {
        
        @foreach (var prop in ViewData.ModelMetadata.Properties.Where(pm => pm.ShowForDisplay && !ViewData.TemplateInfo.Visited(pm))) {
            if (prop.HideSurroundingHtml) {
                @Html.Display(prop.PropertyName)
            } else {
                
            }
        }
        
    @prop.GetDisplayName()
    @Html.Display(prop.PropertyName)
    }

提交回复
热议问题