Pregunta

Estoy buscando crear una plantilla de editor para object.cshtml para cambiar el comportamiento del método html.EditorFormodel (). No puedo encontrar ningún ejemplo de esto usando Razor. He visto este ejemplo Uso de MVC2 y WebForm View Engine, pero no sé lo suficiente sobre Razor para convertirlo. Incluso un ejemplo simple sería muy útil.

¿Fue útil?

Solución

Solo voy a hacer la plantilla de visualización y dejar el resto como un ejercicio para el lector :)

@if (Model == null) {
    <text>@ViewData.ModelMetadata.NullDisplayText</text>
} else if (ViewData.TemplateInfo.TemplateDepth > 1) {
    <text>@ViewData.ModelMetadata.SimpleDisplayText</text>
} else {
    <table cellpadding="0" cellspacing="0" border="0">
    @foreach (var prop in ViewData.ModelMetadata.Properties.Where(pm => pm.ShowForDisplay && !ViewData.TemplateInfo.Visited(pm))) {
        if (prop.HideSurroundingHtml) {
            <text>@Html.Display(prop.PropertyName)</text>
        } else {
            <tr>
                <td>
                    <div class="display-label" style="text-align: right;">
                        @prop.GetDisplayName()
                    </div>
                </td>
                <td>
                    <div class="display-field">
                        @Html.Display(prop.PropertyName)
                    </div>
                </td>
            </tr>
        }
    }
    </table>
}

Otros consejos

Esto parece funcionar para Plantilla de editor Para Bootstrap, hágamelo saber de cualquier mejora

Object.cshtml

@if (Model == null)
{
    <text>@ViewData.ModelMetadata.NullDisplayText</text>
}
else if (ViewData.TemplateInfo.TemplateDepth > 1)
{
    <text>@ViewData.ModelMetadata.SimpleDisplayText</text>
}
else
{
    foreach (var prop in ViewData.ModelMetadata.Properties.Where(pm => pm.ShowForDisplay && !ViewData.TemplateInfo.Visited(pm)))
    {
        if (prop.HideSurroundingHtml)
        {
            <text>@Html.Editor(prop.PropertyName)</text>
        }
        else
        {
            <div class="form-group">
                @Html.Label(prop.PropertyName, new { @class = "control-label col-md-2", @style = "text-align:right;" })
                <div class="col-md-10">
                    @Html.Editor(prop.PropertyName, null, new { @class = "form-control " })
                    @Html.ValidationMessage(prop.PropertyName, "", new { @class = "text-danger" })
                </div>
            </div>
        }
    }
}
Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top