Thanks for your insight @Chao Deng-MSFT
Unfortunately this doesn't help with my specific problem.
Let me try elaborate further.
The control itself has both different HTML surrounding it, and some conditional logic on how it will display
different HTML surrounding it
For example, on some pages it may be a single column layout
<div class="container-fluid px-4">
@foreach (var property in ViewData.ModelMetadata.Properties)
{
<div class="mb-3">
<label asp-for="@property.PropertyName">@(property.GetDisplayName())</label>
@Html.Editor($"{property.PropertyName}", new { htmlAttributes = new { @class = "form-control") } })
</div>
}
</div>
On other's multi columns or more
<div class="container-fluid px-4">
@{var properties = ViewData.ModelMetadata.Properties.ToArray();}
@for (int i = 0; i < properties.Length; i += 2)
{
<div class="row mb-2">
<div class="col-md-6">
@Html.Editor($"{prop.PropertyName}", new { htmlAttributes = new { @class = "form-control" } })
</div>
<div class="col-md-6">
@{var prop = properties[i];}
@Html.Editor($"{prop.PropertyName}", new { htmlAttributes = new { @class = "form-control" } })
</div>
</div>
}
</div>
Conditional logic around the control itself
In order to cater for required fields, readonly fields and boolean fields, I've had to add some additional logic which is now repeated on multiple pages
@{var prop = properties[i];}
@if (prop.ModelType.Name.Equals("Boolean", StringComparison.InvariantCultureIgnoreCase))
{
<div class="form-check">
<label asp-for="@prop.PropertyName">@(prop.GetDisplayName())</label>
@if (prop.IsReadOnly)
{
@Html.Editor($"{prop.PropertyName}", new { htmlAttributes = new { @class = "form-check-input", @type = "checkbox", @disabled = "disabled" } })
}
else
{
@Html.Editor($"{prop.PropertyName}", new { htmlAttributes = new { @class = "form-check-input", @type = "checkbox" } })
}
</div>
}
else
{
<label asp-for="@prop.PropertyName">@(prop.GetDisplayName())</label>
@if (prop.IsReadOnly)
{
@Html.Editor($"{prop.PropertyName}", new { htmlAttributes = new { @class = "form-control", @readonly = "readonly" } })
}
else
{
@Html.Editor($"{prop.PropertyName}", new { htmlAttributes = new { @class = "form-control" } })
if (prop.IsRequired)
{
@Html.ValidationMessage($"{prop.PropertyName}", new { @class = "text-danger" })
}
}
}
This kind of logic is used on multiple pages making this one unmaintainable mess :(
My hope and dreams were to put this messy conditional logic in a partial, then be able to implement it in multiple places.
The Above would then look like:
<div class="container-fluid px-4">
@foreach (var property in ViewData.ModelMetadata.Properties)
{
<div class="mb-3">
Html.RenderPartial("_InputControl", property);
</div>
}
</div>
I hope this clarified my question clearer.
Thanks