0

working in an Asp.Net MVC3 project, using razor engine, I found a situation that I don't know how to handle at best. In a view, I have the following situation:

@foreach (var item in Model.Items) { 
   Html.RenderAction("ActionName", new {param = item});
}

I think it's a very typical situation.

Now let's say that the html portion rendered need a couple of javascript files to work properly, and a css too. And also I want that the partialView of this action should be available in other views as well, one or more times.

The question is: where's the best place to put the references to external js and css? I'd say in the PartialView... but I can I avoid that calling n times RenderAction, doesn't put on the page n times the same script declaration? Moreover, said that the problem of repeating the js and css is solved, how should I bind this external resources with the view? Usually in my views I put the external resources in a dedicated section, that the layout page will render at the bottom of the page, for performance reason. Am I supposed to do the same with the partialView, assuming that the view who call it will have access to the scripts section? But in this way I bind the design of the reusable partialView to the hosting view structure: if so, shouldn't I just declare the scripts and css in the hosting view?

I'm pretty new to MVC and I'd like to know which approach do you use to solve this design issue.

themarcuz
  • 2,573
  • 6
  • 36
  • 53

1 Answers1

0

I usually try to figure out if i need the css or js file in few pages or just one. For example i include jquery file in layout.cshtml just to be assured that every single view in the project,same with the main css files. And then te files that i need only for specific Views i just include those in the whatever the main view is, of course all partials to that view will have it too. If you put a reference in the partial and call it multiple times, you will have multiple references. You can just putbyour references right above your foreach loop and all partials willhave access to it.

bobek
  • 8,003
  • 8
  • 39
  • 75
  • the suggestion to put the reference right before the loop for sure works, but I think it raises a design issue: the main view should know what the partial view needs in order to be displayed fine. Probably this is not good in terms of design pattern... – themarcuz Dec 11 '11 at 21:36
  • Whatdo you mean main view? Is it the view for action in your controller or layout view? – bobek Dec 12 '11 at 03:27
  • I mean the view for the action – themarcuz Dec 12 '11 at 08:55
  • Then just put it at the beginning of that view – bobek Dec 13 '11 at 15:49
  • But if I want to include the partial action in other views, I need to add it in every View. And, if I change the css link my partial action needs, I need to update again all the view that contains that partial action. And if a partial action is called by others partial actions that can be included in different views, everything become even more complicated... I hoped there would be a better approach. – themarcuz Dec 13 '11 at 15:56
  • If you put it up in your action view all the partials that are being loaded in this view will have your reference files included. If you are planing on using this file in few different views, just put it in your master view (_Layout.cshtml) – bobek Dec 13 '11 at 16:02
  • I mark your answer as the right one, since actually you're the only one who proposed something. – themarcuz Feb 22 '12 at 11:01