Results 1 to 6 of 6

Thread: [RESOLVED] Refactoring a partial View.

  1. #1

    Thread Starter
    Fanatic Member
    Join Date
    Feb 2000
    Location
    Dunmow,Essex,England
    Posts
    898

    Resolved [RESOLVED] Refactoring a partial View.

    Hi All,

    I have 2 controllers that have an ActionResult. The ActionResults get called by the respective views and some parameters are set. At the end of each Action result I call a method on each of the controllers in the form:

    Code:
    .....
    PartialViewResult summary = GetSectionSummaryPartialViewResult(sectionControllerName,policyZoneID ,sectionID);
                
                return summary;
            }
    then in both controllers I have the following:

    Code:
    private PartialViewResult GetSectionSummaryPartialViewResult(string sectionControllerName,int policyZoneID, int sectionID)
            {
                PartialViewResult viewResult;
    
                switch (sectionControllerName)
                {
                    case "GoodsInTransitSection" :
                        GoodsInTransitSectionAndRisksViewModel gitViewModel = GoodsInTransitSectionService.GetGoodsInTransitSectionAndRiskViewModel(policyZoneID, sectionID);
                        viewResult = PartialView("SectionSummary/GoodsInTransitSectionSummaryData", gitViewModel);
                        break;
                    case "FrozenFoodsSection":
                       .......
    This all works fine, but at the moment having my code this way means that I have to maintain the method in 2 controllers when I really only want one copy of the method and it be called from both.

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,274

    Re: Refactoring a partial View.

    Controllers are classes like any other so you handle this in exactly the same way as you would for any other classes. In this case, the most obvious solution is to create a base class that inherits Controller and put the common method in that class, then have your two controllers inherit that base class.

  3. #3

    Thread Starter
    Fanatic Member
    Join Date
    Feb 2000
    Location
    Dunmow,Essex,England
    Posts
    898

    Re: Refactoring a partial View.

    I cant place this in a base class as We are already using an abstract class for all our controllers at this level. I dont want to expose this method to everything, only these 2 controllers.

    I know I have it declared as private at the moment. Ideally what I really want to do, would be to push this back to our service Layer, but because I am returning a PartialView, it will break our model.

    In my Controller Folder, we have all our controller files as you'd expect. Can I introduce a file here and call it something like 'SharedController', place my private function here and make it public, and call it from the original 2 controllers?

  4. #4
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,274

    Re: Refactoring a partial View.

    Quote Originally Posted by Bill Crawley View Post
    I cant place this in a base class
    Of course you can. You say that you already have an abstract base class that all your controllers inherit. Simply declare another abstract class that inherits that, add this method to it and then make those two controllers inherit the new class instead of the original one.

  5. #5

    Thread Starter
    Fanatic Member
    Join Date
    Feb 2000
    Location
    Dunmow,Essex,England
    Posts
    898

    Re: Refactoring a partial View.

    Ah, that's my confusion I think. I thought that you could only have 1 base class, I guess that still holds true since I'm not really attempting to inherit from more than 1 base class at once.

    Will Give it a go and see what happens.

  6. #6
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,274

    Re: [RESOLVED] Refactoring a partial View.

    Each class can only inherit one base class, but that base class can also inherit its own base class and so on up the hierarchy. Consider that TextBox inherits TextBoxBase inherits Control inherits Component inherits something else and eventually inherits Object.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width