Reputation: 1459
I'm still getting a grasp on MVC and I'm trying to stay as true to the pattern as possible. I have Model with a property of Status, amongst others. The controller calls the view and passes the appropriate model. On the view if Status = "Incomplete" I need it to show Incomplete <a href="blah">Complete registration</a>
,if Status = "On Waiting List" I need it to show On Waiting List: Position @Model.WaitListPosition
, etc, etc.
The logic to decide what to show based on the status would be in the View since it's determining how it's presented to the user, right? Or should the string be built in the controller and passed to the view?
Upvotes: 0
Views: 117
Reputation: 2583
Separation of concerns dictates that the controller shouldn't be aware of how you display the data, only the model call needed to fetch the data needed by the view, which would include the Status field. The view is concerned wherever logic should dictate how to transform data for display.
Certain frameworks (Ruby on Rails being the only one I know) provide ways to extract the data transformation logic from the view's layout. In RoR's case, they're called helpers. For example, you'd have this helper method called format_status
which takes the Status as its parameter and returns the expected HTML rendering. Note that this allows you to easily write unit tests for that method without having to deal with (more) complicated web view tests.
Upvotes: 0
Reputation: 34349
This logic is best suited for your view - ultimately it comes down to code reuse, and this is specific to a particular view. If you need the same behaviour in several views, it would still be better to have this logic run in the view, either as a partial view, or a custom HTML helper. Keep the logic in your controllers as brief as possible.
Upvotes: 0
Reputation: 17010
Don't build the string in the controller, unless you want your business logic in the controller rather than in your business components. If you absolutely feel the need to put this out as a string, as it is the only way you can come to solve this, have the business component do the actual work.
As for how to branch in a view, it can be as simple as including data for a particular condition and having the view branch based on the existence of that data. Or having a Boolean (or flag of some sort) that indicates where the user is in the process. If that value indicates incomplete, show the "incomplete" logic. That way the View only makes a decision based on binding of data and the controller merely controls the flow and does not make business decisions.
Upvotes: 0
Reputation: 2262
The first is better. The controller should be as thin as possible. Let the view decide how to render the data passed to it.
Upvotes: 2