I have a jquery file that I want to reference and render in in timelime.cshtml. I am attaching the file view below. review attachment. I want reference the jquery file in the attachment below.
The name of the jquery file timeline.js.
MVC4 Reference and render jquery file
18 Answers
Know the answer? Post it. Somebody with the same question will find it here.
Sunny SharmaRank #223 · 11.1K pointsPosted Oct 8, 2013, 11:22 PM EDT
The easiest way is to use the bundling feature that comes with MVC 4.
Open BundleConfig.cs file under App_Start, find this pattern and modify:
------------------------------------------------------------------------
bundles.Add(new ScriptBundle("~/bundles/jquery").Include(
"~/Scripts/jquery-1.10.2.min.js",
"~/Scripts/timeline.js",
));
------------------------------------------------------------------------
That's it. Now it will be rendered automatically wherever you've this snippet in view:
@Scripts.Render("~/bundles/jquery")
basically above line lies in _Layout.cshtml so that jquery file is rendered in each derived page.
Alternatively, if you want to refer this file to any specific page only, then create a script bundle and refer in those pages, like:
Your new script bundle:
-----------------------------------------------------------------------
bundles.Add(new ScriptBundle("~/bundles/timeline").Include(
"~/Scripts/timeline.js",
));
-----------------------------------------------------------------------
Now put this snippet to render this bundle in any view like:
@Scripts.Render("~/bundles/timeline")
Hope you got the point.
Happy Coding :)
David SmithRank #1,300 · 1.3K pointsPosted Oct 9, 2013, 1:34 AM EDT
I changed the second input parameter.
From:
bundles.Add(new ScriptBundle("~/bundles/jquery").Include(
"~/Scripts/jquery-1.10.2.min.js",
"~/Scripts/timeline.js",
));
To:
bundles.Add(new ScriptBundle("~/bundles/jquery").Include(
"~/Scripts/jquery-1.9.1.min.js",
"~/Scripts/timeline.js",
));
However I am still troubleshooting why my timeline feature is not working yet. I wonder if the .js file is being rendered.
Sunny SharmaRank #223 · 11.1K pointsPosted Oct 9, 2013, 1:53 AM EDT
Not to worry about, I'll help you with that.
I assume in my previous post that timescript.js file is present inside Scripts folder. If not, then please move it and try again. If yes, please try to check in the View Source of rendered html if "timeline.js" is being rendered. Just try to find "timeline.js". If it's in there then it is being rendered otherwise not.
If it is being rendered and it's not working as expected then there must me some problem within your timeline.js file codes, suggest you to check that.
If it's not being rendered, check your master layout _Layout.cshtml if it has the snippet at the bottom or top to render the defined script bundle:
@Scripts.Render("~/bundles/timeline")
HTH :)
David SmithRank #1,300 · 1.3K pointsPosted Oct 9, 2013, 6:48 AM EDT
-The timeline.js is located in the script folder.
-By default for the ASP MVC4 template the _layout.cshtml has the @Scripts.Render("~bundles/jquery") at the bottom of the page after the footer.
-I did not add @Scripts.Render("~bundles/timeline") to the _layout.cshtml, because you said if i wanted the timeline.js file to be render on every page just add it to the bundle. I did just that. Review below.
Review attachments below and look at every file.
David SmithRank #1,300 · 1.3K pointsPosted Oct 9, 2013, 6:53 AM EDT
David SmithRank #1,300 · 1.3K pointsPosted Oct 9, 2013, 7:26 AM EDT
Sunny SharmaRank #223 · 11.1K pointsPosted Oct 9, 2013, 9:54 AM EDT
Apart of that, modify this line of code:
---------------------------------------------------
@Model.LoadMoreUrl "')" href="javascript:void(0)">
More
as
More
(Remove the double quotes around @Model.LoadMoreUrl)
HTH :)
David SmithRank #1,300 · 1.3K pointsPosted Oct 9, 2013, 10:03 AM EDT
Sunny SharmaRank #223 · 11.1K pointsPosted Oct 9, 2013, 10:17 AM EDT
What I meant, If timeline.js file has any dependency to this jquery.scrollTo-1.4.1.min.js then do add this to the bundle, otherwise you don't need to.
You can share your project if still have any issues and I'll sort it out.
Thanks.
David SmithRank #1,300 · 1.3K pointsPosted Oct 9, 2013, 4:45 PM EDT
or
Sunny SharmaRank #223 · 11.1K pointsPosted Oct 9, 2013, 9:25 PM EDT
OR
"
My Answer:
Bundling the Script in one bundle gives you freedom to use them wherever you need it and not where you don't.
Please note, that if you create a bundle with the name that is already created, the bundle information would get over-written. What I mean is, If I have added a Script Bundle with name "~/bundles/jquery", which contains jquery-1.9.1.min.js and jquery.scrollTo-1.4.1.js, two files:
AND,
I'm trying to create another one with same name, it gets over-written:
bundles.Add(new ScriptBundle("~/bundles/jquery").Include(
// here it contains only one file - jquery.scrollTo-1.4.1.js.
So now if I try to render this bundle: ~/bundles/jquery, using this statement: @Scripts.Render("~/bundles/jquery"), it will render only a single file jquery.scrollTo-1.4.1.js because this is what we have declared in the last bundle for said name.
Please check if you're adding two bundles with same name. Bundles provide you the flexibility of rendering only required files hence decreasing the bandwidth and increasing the speed, like, if I need jquery UI on any View, I'll render a bundle that contains jquery-ui... .js, otherwise I won't.
Thanks.
David SmithRank #1,300 · 1.3K pointsPosted Oct 9, 2013, 11:21 PM EDT
David SmithRank #1,300 · 1.3K pointsPosted Oct 10, 2013, 12:28 AM EDT
public class BundleConfig { public static void RegisterBundles(BundleCollection bundles) { bundles.Add(new ScriptBundle("~/bundles/jquery").Include( "~/Scripts/jquery-1.*"));"~/Scripts/jquery.validate*")); bundles.Add(new ScriptBundle("~/bundles/modernizr").Include( "~/Scripts/modernizr-*")); bundles.Add(new StyleBundle("~/Content/css").Include("~/Content/site.css")); //TODO: Added new bundlesSunny SharmaRank #223 · 11.1K pointsPosted Oct 10, 2013, 1:52 AM EDT
public class BundleConfig
"~/Scripts/jquery.validate*")); bundles.Add(new ScriptBundle("~/bundles/modernizr").Include( "~/Scripts/modernizr-*")); bundles.Add(new StyleBundle("~/Content/css").Include("~/Content/site.css")); //TODO: Added new bundles"~/Scripts/Timeline.js",
-------------------------------------------------------
Give it a try and let me know if it works or not.
I've skype but not able to connect to you right this moment.
Thanks,
David SmithRank #1,300 · 1.3K pointsPosted Oct 10, 2013, 4:58 AM EDT
@Scripts.Render("~/bundles/jquery")
@Scripts.Render("~/bundles/Timeline")
Sunny SharmaRank #223 · 11.1K pointsPosted Oct 10, 2013, 9:13 AM EDT
Here's my skype id: sunny.programmer
David SmithRank #1,300 · 1.3K pointsPosted Oct 10, 2013, 11:10 AM EDT
Sunny SharmaRank #223 · 11.1K pointsPosted Oct 10, 2013, 9:32 PM EDT
I'm in GMT +5. It's 11:01 now.