1

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 ?

3 Answers 3

1

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

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

1 Comment

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 :)
0

This may help, though it's more for script references than a startup script (read on to the section on resource tracking). http://stephenwalther.com/blog/archive/2008/08/22/asp-net-mvc-tip-36-create-a-popup-calendar-helper.aspx

Comments

0

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

11 Comments

Could you describe context of your situation?
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.
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: blog.codeville.net/2008/10/14/partial-requests-in-aspnet-mvc
|

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.