Reputation: 9279
I have an MVC3 C#.Net web app. I have two views that display the same HTML table. However, each View represents a different Model. View 1 = "ProposalEdit", View 2 = "DocEdit". Both the Proposal model and the Doc model have a property:
ICollection<Deliverable> Deliverables.
In each of the Edit Views, I display the Deliverables belonging to the Model of that View. It's identical code in each of the Edit Views, only the Model behind the Edit Views is different. So, there's a lot of duplicate code.
How can I modularize this in order to reduce the duplicate code?
Upvotes: 0
Views: 297
Reputation: 338
@Mike, that is exactly what I would do. The main point though is having the DisplayTemplates for each model. Even a DateTime object could have a DisplayTemplate if you wanted to.
Edit:
Here's an example of using the DisplayTemplate for just the Deliverables model
@model ICollection<Deliverables>
@foreach (var deliverable in this.Model)
{
@this.Html.DisplayFor(d => deliverable)
}
You would also, of course, have a template under ~\Shared\DisplayTemplates\Deliverables.cshtml
or for editing under ~\Shared\EditorTemplates\Deliverables.cshtml
Upvotes: 1