Creating an object.cshtml editor template with MVC and Razor

后端 未结 2 1508
不知归路
不知归路 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:20

    This seem to work for Editor Template for bootstrap, please let me know of any improvements

    Object.cshtml

    @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.Editor(prop.PropertyName)
            }
            else
            {
                
    @Html.Label(prop.PropertyName, new { @class = "control-label col-md-2", @style = "text-align:right;" })
    @Html.Editor(prop.PropertyName, null, new { @class = "form-control " }) @Html.ValidationMessage(prop.PropertyName, "", new { @class = "text-danger" })
    } } }

提交回复
热议问题