Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8176035
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T23:00:03+00:00 2026-06-06T23:00:03+00:00

I have two actions in my controller which are sharing part of logic responsible

  • 0

I have two actions in my controller which are sharing part of logic responsible for selecting the view. How Can I make this part common accross actions. Example:

Controller Document

  • Action Open

    1. if there is one document found and is type X, display it using OpenX View
    2. if there is one document found and is type Y, display it using OpenY View
    3. if there are more than documents found, display list using List View
    4. if there are no documents found, display error using Error View
  • Action OpenMetaData

    1. if there is one document found, display it using OpenMetaData View
    2. if there are more than documents found, display list using List View
    3. if there are no documents found, display error using Error View

As you can see, points 3,4 are the same as 2,3

I would like to create something like

public DocumentController
{

    public ActionResult Open( ... )
    {
       var dataFromWebService = service.GetData( ... );
       return ViewSelector.GetLaunchView(dataFromWebService);
    }

    public ActionResult Open( ... )
    {
       var dataFromWebService = service.GetData( ... );
       return ViewSelector.GetOpenMetaData(dataFromWebService);
    }
}

public class ViewSelector
{
     public static ActionResult GetLaunchView(DataFromWebService dataFromWebService)
     {
          if( dataFromWebService contains document type X)
              return new ViewResult("OpenX",data);
          if( dataFromWebService contains document type Y)
              return new ViewResult("OpenY",data);
          return CommonLogic(dataFromWebService);
     }

     public static ActionResult GetOpenMetaData(DataFromWebService dataFromWebService)
     {
          ......
     }

     private static ActionResult CommonLogic(DataFromWebService dataFromWebService)
     {
          .... Common logic
     }
}

I would like to do this to make my Controller as clean as possible.

Can I create ViewResults outside controller, attach data to them are return them in the action ?

Is this good or bad design ?

Maybe someone have better idea how to handle this

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-06T23:00:10+00:00Added an answer on June 6, 2026 at 11:00 pm

    If you don’t need to access any of the contexts of the controller you can create the results outside the controller. In your case I would consider making the GetOpenMetaData() and GetLaunchView methods private methods of the controller.

    If you need to share it accross multiple contollers you could also consider putting it into an abstract BaseController and let your controllers inherit from it.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a controller with two different actions, but both need this same code,
Say if I have a controller, profile, which has just two actions. The first
I have two actions in my controller : def find @item = Item.find(params[:id]) render
Hi, For a view that will be submited I have two actions with the
I have two very similar specs for two very similar controller actions: VoteUp(int id)
I have a Play! framework with two actions which contain redundant code. So I
I have two controller actions outlined below: public ViewResult TitleStorylines(int id) { var vm
I have a controller tavern and in the index there can be some actions.
I have this simple MVC 3 application which simply has two controllers(Home and Edit),
Let's say I have a hypothetical controller called ThingsController that exposes two actions: index

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.