I've got a lot of booleans in my model, and we're using Bootstrap, so for every boolean property I'm copy/paste refactoring:
<div class="form-group">
<div class="custom-control custom-checkbox ">
<input asp-for="IsFoo"/>
<label asp-for="IsFoo"></label>
</div>
</div>
... but that's dumb. I tried adding this to Views/Shared/EditorTemplates/bool.cshtml:
@model bool?
<div class="form-group">
<div class="custom-control custom-checkbox ">
<input asp-for="@Model"/>
<label asp-for="@ViewData.TemplateInfo.FormattedModelValue"></label>
</div>
</div>
... and calling it with @Html.EditorFor(m => m.IsFoo) but all I'm getting back is a plain input element from the default template.
what am I doing wrong herename the template 'boolean.cshtml'isnope.ViewData.TemplateInfo.FormattedValuethe right value to get theDisplay(Name="xxx")Attribute from the propertyViewData.ModelMetadata.DisplayName- is there some new & improved version instead of Editor Templates in ASP.NET Core that I should be using (like Tag Helpers?) instead of the "old" way, and if so, how do I go about it?
Use the
<partial>tag-helper:It works with binding properties too:
Change your partial-view to:
The
for=""attribute causes the name/id/binding context in the partial to match the named property, so ASP.NET will do the magic to ensure that<input asp-for="@Model" />will correspond toModel.MyCheckBoxList[0]and so on.