没有必需属性需要Bool,DateTime和Int?

时间:2013-01-07 18:38:45

标签: asp.net-mvc-4 mvc-editor-templates

我正在使用如下的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>
    }
}

1 个答案:

答案 0 :(得分:2)

尝试将非必填字段更改为可空:

public int? MyInt { get;set; }
public DateTime? MyDateTime { get;set; }
public bool? MyBool { get;set; }