Question

I am using an Object.cshtml like below and for some reason a normal int property like this public int MyInt { get;set; } are required even though they are missing a required attribute, how come? Am I missing something in my editor template?

@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>
    }
}
Was it helpful?

Solution

Try changing not required fields to nullable:

public int? MyInt { get;set; }
public DateTime? MyDateTime { get;set; }
public bool? MyBool { get;set; }
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top