Using partials to automate form inputs creation
My main focus is having as little duplicate code/files as possible. Most of the HTML I write is almost identical. There is already a form template which holds the form and several necessities. That leaves the elements in the form which are all the same expect for the values in asp-for, asp-items and placeholder attributes. Currently I need to create a create and edit partial for every page. e.g. the page User will have CreateUserPartial and EditUserPartial. These partials always contain all the elements from the model (create and edit model may differ). For example if I have a ViewModel like: public class ExampleCreateModel { public string Name { get; set; } public List ThoseItems { get; set; } public List TheseItems { get; set; } } I have to create a Partial with 3 of the following html elements (Example of a select element): Given this, I’m considering automating form generation by creating a template-driven "engine" that gets passed a ViewModel and dynamically generates the form elements. Would this be a reasonable design pattern, or would it be over-engineering/misusing Razor? Are there better alternatives for reducing duplication in Razor views?

My main focus is having as little duplicate code/files as possible. Most of the HTML I write is almost identical.
There is already a form template which holds the form and several necessities. That leaves the elements in the form which are all the same expect for the values in asp-for
, asp-items
and placeholder
attributes.
Currently I need to create a create
and edit
partial for every page. e.g. the page User
will have CreateUserPartial
and EditUserPartial
. These partials always contain all the elements from the model (create and edit model may differ).
For example if I have a ViewModel like:
public class ExampleCreateModel
{
public string Name { get; set; }
public List ThoseItems { get; set; }
public List TheseItems { get; set; }
}
I have to create a Partial with 3 of the following html elements (Example of a select element):
Given this, I’m considering automating form generation by creating a template-driven "engine" that gets passed a ViewModel and dynamically generates the form elements.
Would this be a reasonable design pattern, or would it be over-engineering/misusing Razor? Are there better alternatives for reducing duplication in Razor views?