4

I'm a web forms programmer who's interested in learning a bit about MVC. I have created a whole suite of web controls which can access the page header directly and write stuff in there. For example, if I have a control to render a text box with datepicker functionality, it can access the page header in order to automatically add links to the JavaScript and CSS files it needs. I love this, because I'm too lazy to think about what linked files I need. A lazy programmer is a good programmer, right?

My question is, is there a way to do this in MVC? That is, to create a custom HtmlHelper (for example) which, as well as rendering the control markup on the page, can render the script and link tags it requires into the page header?

David
  • 15,750
  • 22
  • 90
  • 150

4 Answers4

4

With MVC3 in your _Layout.cshtml (basically master pages) you use @Render.Partial("Header", required: false)

Then you can use named sections in your views.

@section Header {
    @{ Html.MyHelper.GetResources(); }
}

@Html.MyHelper.DoSomething()

With required: false this means MVC3 won't error if the view doesn't have a named section for Header. If you want to require ALL pages have a named section (for example bread crums) you can use required: true (which is the default) and if a view doesn't have the @section Header it would error.

Chris Marisic
  • 32,487
  • 24
  • 164
  • 258
  • I think you misunderstood the question. – Robert Koritnik Mar 18 '11 at 12:48
  • @Chris: As much as I understand his question he's not talking whether it's possible to render to `HEAD` section of a page but whether it's possible to white in the main view's `HEAD` from within a partial view that's rendered within this parent view. `HtmlHelper` adds content in-place and not in other parts of the page. – Robert Koritnik Mar 18 '11 at 13:03
  • /shrug, there's no difference, i updated it to be more respective for working html helpers. – Chris Marisic Mar 18 '11 at 13:39
  • @Chris Marisic: In views yes because they use some layout *master*. But is it possible to render into sections from partial views? [This thread](http://forums.asp.net/t/1654507.aspx/1) says you can't. – Robert Koritnik Mar 18 '11 at 13:48
  • @Robert Koritnik reading the accepted answer on your posted link seems to iterate exactly what I said here. – Chris Marisic Mar 18 '11 at 15:19
0

In web forms you have custom server controls, which get page object and work with it? Asp.net mvc does not have server controls and Page object.

As for me it's bad idea to link resources such way - in client optimization best practice - open the least number of requests to the server for resources. You could download YSlow to improve perfomance of your site.

But if your want you could create extension method and use it in view or create actionfilterattribute, which set some url strings;

Antony Blazer
  • 705
  • 3
  • 19
  • Well this still doesn't answer his question. Your answer is more about scripts combination to reduce number of requests than adding script references inside `head` element. – Robert Koritnik Mar 18 '11 at 12:49
  • Yes. You are write. But i don't know how he decide to render server controls in asp.net mvc: is it partial views or render action or something else. Another way to determine css or scripts - create on start application event static dictionary with key - controller, value - array of strings(resources). In View he could use this like Helper.MyDictionary[ViewContext.RouteData.Values["controller"] as string] – Antony Blazer Mar 18 '11 at 13:03
0

Yes, - Telerik does a great job of doing this with its ScriptRegistrar and Stylesheet registrar. A good starting point is the web asset documentation

smartcaveman
  • 41,281
  • 29
  • 127
  • 212
-1

A sort of script manager

I've found this resource on the internet that's partially related to your problem and may be the most that can be done in this regard.

The solution doesn't really render in the HEAD element but it helps adding scripts only once from within partial views and rendering them in the main view. So you wouldn't have to worry about your partial view scripts. They'd take care of themselves (being more encapsulated that is)

A Simple ScriptManager for ASP.NET MVC

Custom view engine approach

But otherwise I suppose it could be possible by saving references in a dictionary (similar to how the upper link does) and then have your own view engine that renders head part in the end rendering script references.

Custom ViewPage class approach

You could as well write your won classes for views and partial views. This way you could change the way that a view renders which would again use some sort of dictionary. You could fill that dictionary via Html helper extension method or create a custom ViewUserControl class that would have this kind of functionality.

Of the three I suppose the last one is the simplest and could write inside HEAD element. You could also provide functionality in it that would do resource combining.

Robert Koritnik
  • 103,639
  • 52
  • 277
  • 404