4

I have a website built using a MVC.NET framework with C# that uses some shared views and partial views. In order to display the views properly there is a bit of code that must reside in the controller. Currently I have that logic duplicated between controllers.

An example would be an view that takes in an error id and then pulls down the record from the database and formats the view differently based on the error type. There is a small amount of work that needs to be done to the data in the controller before it can be displayed.

If the view is in the Shared directory where would the controller for it go? Or, is it better design to create a new controller just for it?

I am trying to choose a design that will prevent the "controller creep" that we had in a prior project.

Update: I am currently learning about MVC using Razor and learned about the HTML.Action extension method. If I were going to go back and rewrite this project I would put the method into a "Shared" controller and use HTML.Action to format the data to display it on what ever screen I needed it.

1
  • 1
    Can you specify the particular language or framework that you're working with? This will make it easier to guide you.
    – Gary
    Apr 25, 2011 at 18:53

2 Answers 2

3

Try to change your viewpoint when you're thinking about this problem. A view doesn't have an action. An action returns a reference to a view. Multiple actions may return references to the same view, or actions may not return a response which isn't related to a view. That's the relevant part of an MVC pattern.

In MVC for ASP.NET, using the default ViewEngine, if an action returns a reference to a view (using return View("ViewName")) then the framework searches first in a folder named after the Controller and then in a folder named "Shared", both in the application's View folder.

So the question "If the view is in the Shared directory where would the controller for it go?" doesn't really make sense. What you should consider is that a view is put into the Shared directory so that it can be accessed by multiple controllers.

6
  • "Multiple actions may return references to the same view ..." This is true and I understand it. But, my question is about where to put the code when I want to create a single action to reference a view. I always want ActionMethodA() to be used to display ViewA. Apr 25, 2011 at 21:46
  • @Amy P - Then don't put ViewA in Shared, put it in the folder named after the controller
    – pdr
    Apr 25, 2011 at 21:50
  • @pdr The problem is that I need for ViewA to be used by multiple controllers but still only be called through ActionMethodA. Apr 25, 2011 at 22:01
  • @pdr Maybe my problem is that I need to put more of the logic in the ViewModel. Up until recently I never put logic in the viewmodel but kept all logic in either a service layer or the controller. Ahhh, the circles are making me dizzy. Apr 25, 2011 at 22:03
  • @Amy P - I don't understand. An Action can only exist in one Controller. If you mean a helper method called in each action then it doesn't matter where it exists relative to the View. If you mean an action common to many areas (which I think you do, based on your example) then put it in its own Controller. These shouldn't be common enough to cause Controller Creep.
    – pdr
    Apr 25, 2011 at 22:12
-1

What about this?

public ActionResult SomeAction ()
{
    return View ("~/Views/Shared/Error");
}

Check out this question:

Can I specify a custom location to “search for views” in ASP.NET MVC?

Not the answer you're looking for? Browse other questions tagged or ask your own question.