1

I am really digging this new Razor View Engine... For someone just coming from another web platform the new MVC 3 efforts are starting to feel right at home for me...

My question is about sections, I see it is possible to define sections in your layout and inject content into them but from all the samples I have seen it appears that this is done from the view. For me it makes more sense to assign content or output for each section from the controller.

So if I have a layout that contains a side bar that I want to load widgets into my view file should not be responsible for rendering the content into those sections. If this were the case then if the side bar content was shared across multiple views then I would have to duplicate that code across view files. My view files should be ignorant of what content is rendered in the side bar next to them.

Ideally I would like to assign content to my sections in the side bar from my controller so my question, is this possible?

6
  • 2
    Why would it make more sense to do this from the controller? Controllers generally aren't supposed to have any view logic; they're just responsible for picking a view and handing it a model. Commented Nov 19, 2010 at 3:20
  • why don't you create a partial view and use that in the views as required? Commented Nov 19, 2010 at 3:26
  • controllers do have view logic if you think about it, they init the view model, assign values to it in some cases and then call the corresponding view that uses it. The same should be done for sections. Commented Nov 19, 2010 at 3:52
  • Also wanted to add that the content getting injected into each section may have it's own model, so the controller is going to have to make sure each model is available for the view. Commented Nov 19, 2010 at 4:18
  • When it comes to controllers versus views I tend to think that controllers decide what data to render and the views decide how to render it. I like this kind of separation of concerns a lot. But there's still the Model part of MVC. In my opinion Models (not ViewModels) and Views should be pretty much separate. Models know HOW to get the data (Controlles just tell them to do it) and Views know how to display it, but don't have a clue where it came from. Commented Nov 19, 2010 at 5:42

1 Answer 1

1

In ASP.NET MVC (and other MVC based web application architectures) controllers prepare the data that is rendered by the views. How, where and maybe even when this data is rendered is the view's job to figure out. This is of course more of a architectural design discussion.

A nice thing about ASP.NET MVC is their 'convention over configuration' standpoint which (especially with Razor) lets you change how a lot of things behave. I don't think there's a View.Sections["section1"] = <some data>; option, but you could make a ViewModel that is based on sections, then have some generic master view that just takes apart this ViewModel and sends the correct data to correct partial views. This view model for sections could even hold HTML that has to be rendered, as Razor can be used made to render HTML outside .cshtml files. For more go to Andrew Nures' blog: http://blog.andrewnurse.net/

As you can see this transfers a lot of responsibility from view to the controllers. I myself like that rendering is totally separated from the controllers as then I can hypothetically build very different websites just by changing how data is rendered.

Sign up to request clarification or add additional context in comments.

3 Comments

I use view models and I guess I am not understanding how to use a view across multiple actions while utilizing a view model. Based on what you guys are saying I would either have to copy the same code across multiple view pages or use the same view page across multiple actions. Or possibly copy code across the view model for each view. i am not buying the idea that I should put code in my view "possibly business logic" that decides what gets rendered where and when for a particular view.
If i wanted to assign modules or widgets to output in particular sections on a page where the determination of what renders in which section is in a data table I would not process these decisions from code in my view.
There are a few ways you could separate the responsibility of a view towards it's content. The Html.Action(...) can be used to get the code of another action rendered in the current view. You would probably still have to do some minimal logic in the view that tells this action about the state of you app. But you could of course precalculate this and just boil it down to flags. But as I said try looking at Andrew Nurse's blog post about using Razor outside ASP.NET and it might give you some ideas.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.