Pergunta

Estou procurando criar um modelo de editor para object.cshtml para alterar o comportamento do método html.editorformodel (). Não consigo encontrar nenhum exemplo disso usando Razor. Eu tenho visto este exemplo Usando o mecanismo MVC2 e Webform View, mas não sei o suficiente sobre o Razor para convertê -lo. Mesmo um exemplo simples seria muito útil.

Foi útil?

Solução

Eu só vou fazer o modelo de exibição e deixar o resto como um exercício para o leitor :)

@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>
}

Outras dicas

Isso parece funcionar para Modelo do editor Para o bootstrap, informe -me sobre quaisquer melhorias

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 em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top