我有一个包含不带一个布尔模型[Required]
属性
public bool IsOptedIn { get; set; }
我已经重写Object.cshtml
如下,并正在使用@Html.EditorForModel()
来生成自己的状态
@{
var properties = ViewData.ModelMetadata.Properties
.Where(prop => prop.ShowForEdit && !ViewData.TemplateInfo.Visited(prop));
}
@foreach (var prop in properties)
{
var hasModelStateError = ViewContext.ViewData.ModelState.Any(m => m.Key == prop.PropertyName)
&& ViewContext.ViewData.ModelState[prop.PropertyName].Errors != null
&& ViewContext.ViewData.ModelState[prop.PropertyName].Errors.Count > 0;
<div class="control-group
@(hasModelStateError ? "error" : string.Empty)
@prop.PropertyName.ToLower()">
@if (prop.IsReadOnly)
{
<b>@prop.GetDisplayName()</b>
@Html.Display(prop.PropertyName)
}
else if (prop.HideSurroundingHtml)
{
@Html.Editor(prop.PropertyName)
}
else
{
<label class="control-label">
@prop.GetDisplayName()
@if (prop.IsRequired)
{
<span class="required">*</span>
}
</label>
<div class="controls">
@Html.Editor(prop.PropertyName)
@if (hasModelStateError)
{
<p class="alert alert-block">
@Html.ValidationMessage(prop.PropertyName)
</p>
}
@if (!string.IsNullOrWhiteSpace(prop.Description))
{
<p class="help-block">
@prop.Description
</p>
}
</div>
}
</div>
}
我发现,在我的模型布尔变量的要求总是被标记。 这是为什么,我怎么能阻止它发生?