我期待建立一个编辑模板Object.cshtml改变Html.EditorForModel()方法的行为。我无法找到这个使用剃刀的任何实例。我所看到的这个例子使用MVC2和Web窗体视图引擎,但不知道有足够的了解剃须刀转换。即使是一个简单的例子是非常有益的。

有帮助吗?

解决方案

我只是做显示模板,剩下的作为练习读者:)

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

其他提示

这似乎工作的编辑模板作为引导,请让我知道的任何改进

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>
        }
    }
}
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top