Opinionated Input Builders for ASP.Net MVC – Part 4 the Partial View Inputs
- Part 1 – Overview
- Part 2 – the Label
- Part 3 – the Source Code
- Part 4 – the Partial View
- Part 5 – the Required Field Indicator
This is the forth part of the series.
Given this form that is generated by the input builders the partials for each data type of the Model are selected by the builder based on a simple convention.
Each partial control is selected using the following convention:
- The type of the property is used to find a Partial View by the same name.
- If the partial does not exist in the /Views/Shared/ directory it is pulled from an embedded resource from the Input Builder assembly. This allows you to override the conventions of the framework with your own.
- If a PartialView Attribute is applied to a property it is used to override the previous conventions.
- Display a partial using a Chained Method on the Input() method UsingPartial( partialName )
Looking at the types of the model you can see that the Enum partial will render a Select element by default. The PartialView(“RadioButtons”) attribute was applied to override the default and have the build render the RadioButtons.aspx partial view.
This is an example of using the Chained Method override instead of using an attribute based approach.
This convention of utilizing the partial views allows the separation of concerns (SoC) of the markup from the conventions which determine which partial to render.
What if you do not like the convention ????? Each part of the UI model is extensible. There are two conventions that are at play here. The selection of the partial view and the selection of the data that is used by the controls to display the possible selections. This is shown for the enum data types which allows the select and radio button inputs to display a list of possible values.
The ModelPropertyFactory class calls two methods to execute the conventions. The ParialNameConvention and ModelPropertyBuilder by using the static Func fields this will allow your project to define your own conventions if you do not like mine. This is an important extensibility point that gives the great flexibility to this approach. There are conventions like this for each of the elements used within the partials.