1

我正在使用像下面这样的 Object.cshtml,出于某种原因,public int MyInt { get;set; }即使它们缺少必需的属性,也需要像这样的普通 int 属性,怎么会?我的编辑器模板中是否缺少某些内容?

@foreach (var prop in ViewData.ModelMetadata.Properties.Where(pm => pm.ShowForEdit && !ViewData.TemplateInfo.Visited(pm))) {
    if (prop.HideSurroundingHtml) {
        <text>@Html.Editor(prop.PropertyName)</text>
    } else {
        <div>
            <div class="editor-label">
                @if(prop.IsRequired) {
                    @Html.LabelFor(m => prop, @<em>(mandatory)</em>, prop.GetDisplayName())
                } else {
                    @Html.Label(prop.PropertyName)
                }
            </div>
            <div class="editor-field">
                @Html.Editor(prop.PropertyName)
            </div>
        </div>
    }
}
4

1 回答 1

2

尝试将不需要的字段更改为可为空:

public int? MyInt { get;set; }
public DateTime? MyDateTime { get;set; }
public bool? MyBool { get;set; }
于 2013-01-07T18:49:27.873 回答