0

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?

MikeTWebb
  • 9,149
  • 25
  • 93
  • 132
  • You're probably looking for something like this: http://stackoverflow.com/a/7495623/438275 – Josh Sep 24 '12 at 19:07
  • @Josh...interesting! Looks like I would wrap Proposal and Doc inside the ViewModel and then create the one shared DisplayTemplate for the Deilverables table. Is that how you read it? – MikeTWebb Sep 24 '12 at 19:16
  • 1
    @Mike, I don't even know that you want to wrap proposal and doc...just a shared display template for the ICollection type should do it. – KennyZ Sep 24 '12 at 19:19
  • @KennyZ...I can see that. I'll try both. – MikeTWebb Sep 24 '12 at 20:03
  • I actually think the issue is a little tougher? We've been discussing a generic DisplayFor template. It will apply to all PersistentGenericSet`1 lists. I only want something specific to the Deliverables. – MikeTWebb Sep 24 '12 at 21:02

1 Answers1

1

@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

Josh
  • 338
  • 3
  • 14
  • @Josh...I actually think the issue is a little tougher? We've been discussing a generic DisplayFor template. It will apply to all PersistentGenericSet`1 lists. I only want something specific to the Deliverables. – MikeTWebb Sep 24 '12 at 21:02
  • Make the DisplayTemplate for Deliverables model then and loop through the collection. See my above edit. – Josh Sep 24 '12 at 21:12
  • @Josh...thanks for the update. I'll give it a run...question: how do I call that code above from my ProposalEdit view? – MikeTWebb Sep 24 '12 at 21:52