tags:

views:

116

answers:

2

hi, in Web Forms it was easy to register a script by this method, and to avoid duplicating a script you just use !IsStartupScriptRegistered() in and If statement.

how can you do this in MVC ?

thanks in advanced.

A: 

I guess there isn't 1:1 copy of this functionality.

Some code:

<%@ Control Language="C#" 
    Inherits="System.Web.Mvc.ViewUserControl<IList<Item>>" %>
<script src="../src/preciousJavascript.js" type="text/javascript"/>
<%foreach (var item in Model){%>
<% Html.RenderPartial("_Item",item); %>
<%}%>

<%@ Control Language="C#" Inherits="System.Web.Mvc.ViewUserControl<Item>" %>
<%=Item.Name %>

Maybe this clears something

Arnis L.
any alternative man
Could you describe context of your situation?
Arnis L.
man it is simple and clear as i mentioned, but to elaborate more imagine you have multiple user controls that will use a JavaScript reference, now i put the JavaScript reference or code inside the user control, this is fine, however if you have more than one of these user controls in the page, then you are facing a case of duplication.i hope i was clear.by the way i did note Vote you down !
I don't care if some1 votes me down. Just wanted to help. Anyway - there isn't such a thing like user control in asp.net mvc, you probably mean partial views. Create container partial view which includes external JS reference. Then just add foreach in container partial view over list of objects, which renders item partial views.
Arnis L.
thanks for helping man, i understand what you mean, i have no problem in the partial view, i have problem that if this partial view is referencing a java script file that another partial view is also doing so, in that case you will end up having link to your java script files twice.your solution is good, but the problem is - i don't want to reference a script unless it is needed so i don't want to put all scripts on container. - and again if i put it on partial view A, and Partial view B also reference it then i will get 2 java scripts references. what i want is a portal like site. -
You can't extract javascript referencing tag to view?About widgets, this one could interest you: http://blog.codeville.net/2008/10/14/partial-requests-in-aspnet-mvc/
Arnis L.
One alternative i do know, but i dislike it. Reference your external JS through javascript (via document.write(...)). Define a key in external javascript as variable and initialize it - on including, check if variable is initialized. But i think there must be another way. :)
Arnis L.
thanks a lot man for answering, but what you mean by extracting the Javascript to the view ? about the link, i know about it, and i am more confused here is a link to another post of mine on same issue http://stackoverflow.com/questions/881422/-frustratation-finding-the-right-way-to-create-portal-like-site-with-portlet
I mean - include your external JS in the view, which contains 1 or more partial views. You can include it even in layout (masterpage).
Arnis L.
yeha i know man, but the thing is, i only want to include it when a certain widget needs it, what i mean is if a widget is loaded based on request then the CSS and script related to it should load and must be loaded once in case other similar widgets are loaded too.
I guess i don't know a perfectly clean approach for this. Good luck on hunting it. :)
Arnis L.
thanks alot bro for hanging in with me, i hope i find an answer :)
+1  A: 

Quite simple, you may accomplish some custom reder of js code . It may render teh content at the end of the page and mainatin a series of Key-value fierls. So when you make a cjeck for !IsStartupScriptRegistered() , use Key as parameter. Pretty simple. So I can mention here some custom implementations for this thing, but I wouln't :) try to do it by hand, I know you'll enjoy it! good luck

diadiora
hahah, man i would defiantly enjoy it if i know exactly how to do it :) i am new to MVC plus don't get me wrong, but there are some word in your reply which i don't understand because of the spelling, so if you have time please elaborate more on the subject :)