I am a bit confused about how to implement EditorForModel. I get the idea of setting up templates in Views\Shared\EditorTemplates, but I think I have over-researched (if that's possible) which has led to my confusion.
Basically, I am trying to minimize code replication. I have 5 services I provide to clients, and each service has between 4 - 6 different solutions. Making things more complex, there are certain types that can be shared among many of the solutions to varying degrees.
To make this simple, take the type:
public string FirstName { get; set; }
I will use this in every solution (and going up the "tree" in every service). But obviously I have numerous other types that will either be shared among a few solutions or none at all.
Let's say I have 100 types for each service. I could separate them out as different View Models, but then I would have to factor that by the 5 services. I would prefer to just keep them all in one View Model, per service.
My trouble comes in when I try to create the templates. I want to use the templates because I am using a wizard and I don't want to re-create a View for each part of the wizard in a particular solution especially if it's the same "data" (and, again, factoring in the amount of solutions I have).
So, I create a BasicDetails.cshtml View (with types for things like FirstName, LastName and Email), which can actually be used across all the solutions. An editor template seems ideal here, but my confusion arises because if I use ServiceViewModelOne.cs and do an @Html.EditorForModel(), I would either spit out all 100 types or, using a template, only spit out that which I want in BasicDetails.cshtml by creating a template at \Views\Shared\EditorTemplates\ServiceViewModelOne.cshtml.
Now, would I be able to, for example, do the following:
@Html.EditorForModel("BasicDetails")
@Html.EditorForModel("WizardStep2")
@Html.EditorForModel("WizardStep3")
for every custom template I wanted to make (and however many more I wanted to make), all using the same ServiceViewModelOne.cs? If I am correct, I would then have the following folder structure:
- \View\Shared\EditorTemplates\BasicDetails.cshtml
- \View\Shared\EditorTemplates\WizardStep2.cshtml
- \View\Shared\EditorTemplates\WizardStep3.cshtml
- ...
Obviously, within each template I would have the corresponding
<div class="editor-label">
@Html.LabelFor(m => m.FirstName)
</div>
<div class="editor-field">
@Html.EditorFor(m => m.FirstName)
@Html.ValidationMessageFor(m => m.FirstName)
</div>
...
for all of the HTML inputs specific to that step in the wizard (which includes the entire HTML layout)
Would this be an appropriate way to do what I want, or am I barking up the wrong tree?
UPDATE: I thought about using UIHint, but I am not sure if it will work completely.
I can use UIHint at the model level:
[UIHint("BasicDetails")]
public class MyModel {}
or at the type level
public class MyModel
{
[UIHint("FirstName")]
public string FirstName { get; set; }
}
And I could create a MyModel.cs and then auto map some of the types in there into MyViewModelOne.cs, MyViewModelTwo.cs, etc.
However, I think I will wind up boxing myself in, so to speak.
Let's say I have Type1, Type2, Type3, Type4. Type1 can be used in every solution (keep it simple, lets say there are 4 solutions). Type2 and Type3 can be used in one solution, but not the others. And lets say that in another solution I can use Type2 and Type4.
Using UIHint or automapping means I'd have to create 3 different things to account for the above, and what if I need to use Type3 and Type4 in yet another solution.
I could be off base but it just seemed to restrictive forcing me to create multiple permutations, which is what I was hoping to avoid.
I will confess to not having used AutoMapper and am looking into whether or not that will help out.