Generally,the default template in layout has this line:
@RenderSection("scripts", required: false)
When you define an @section somewhere, lets say the _Layout.cshmtl file,
it allows all of your Views to dynamically insert script files or CSS files or what ever into places in the defining page.
This is very nice when, for example, you are using the jquery.treetable.js control only on a couple views in your site.
So you may not want to globally include the jquery.treetable.js script file in your _Layout.cshtml since you are only going to need it on
2 or 3 pages.
@section allows you to include those files only for certain views.
It is needed since, a view cannot easily change the contents of the _Layout.cshtml otherwise.
amithashenoy
though i have referred the below js file in _Layout.cshtml, i was getting Javascript run-time error "$" is undefined.
According to your description,you have a jquery library in Layout.cs,like:
MSDN Community Support
Please remember to click "Mark as Answer" the responses that resolved your issue.
If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.
As we all know, when adding the JavaScript/JQuery script, we need to make sure we have already loaded the JQuery reference first, and the reference should be in the front of our JQuery script.
If you want to put the JQuery reference in the _layout. cshtml, you could add it in the header.
Besides, you could also try to use jquery-1.10.2.min.js file.
Best regards,
Dillion
.NET forums are moving to a new home on Microsoft Q&A, we encourage you to go to Microsoft Q&A for .NET for posting new questions and get involved today.
Member
59 Points
196 Posts
refering .js files
Dec 20, 2018 10:38 AM|amithashenoy|LINK
Hi ,
I have this doubt , though i have referred the below js file in _Layout.cshtml, i was getting Javascript run-time error "$" is undefined.
But again i referred same script in page error is resolved .So i wanted to know how this referring works
All the Best . Please 'Mark this Post as Answer ' if it helps you.
All-Star
120146 Points
27989 Posts
Moderator
MVP
Re: refering .js files
Dec 20, 2018 02:19 PM|ignatandrei|LINK
Do not use
$
before loading
That's the idea why
@RenderSection("Scripts", required: false)
exists.
Read more at: https://docs.microsoft.com/en-us/aspnet/core/mvc/views/layout?view=aspnetcore-2.2 ( applices to MVC also)
Contributor
3710 Points
1431 Posts
Re: refering .js files
Dec 21, 2018 03:12 AM|Yuki Tao|LINK
Hi amithashenoy,
For example:
View:
Generally,the default template in layout has this line:
@RenderSection("scripts", required: false)
When you define an @section somewhere, lets say the _Layout.cshmtl file,
it allows all of your Views to dynamically insert script files or CSS files or what ever into places in the defining page.
This is very nice when, for example, you are using the jquery.treetable.js control only on a couple views in your site.
So you may not want to globally include the jquery.treetable.js script file in your _Layout.cshtml since you are only going to need it on 2 or 3 pages.
@section allows you to include those files only for certain views.
It is needed since, a view cannot easily change the contents of the _Layout.cshtml otherwise.
According to your description,you have a jquery library in Layout.cs,like:
For example,if you run the Index action,
the Compiler will check whether the Index view references Layout.cs.
If there is no @section scripts{},this view cannot call all reference packages from Layout.cs.
So, it reports error "$" is undefined.
But if you add the below js file in Index View:
This js file will work for this View.
Best Regards.
Yuki Tao
Please remember to click "Mark as Answer" the responses that resolved your issue.
If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.
Member
59 Points
196 Posts
Re: refering .js files
Dec 26, 2018 10:06 AM|amithashenoy|LINK
actually , it did not clear my doubt.
index file able to refer other script .js , .css file ,had issue only with this particular js file
All the Best . Please 'Mark this Post as Answer ' if it helps you.
All-Star
45489 Points
7008 Posts
Microsoft
Re: refering .js files
Dec 28, 2018 03:22 AM|Zhi Lv - MSFT|LINK
Hi amithashenoy,
As we all know, when adding the JavaScript/JQuery script, we need to make sure we have already loaded the JQuery reference first, and the reference should be in the front of our JQuery script.
Like this:
If you want to put the JQuery reference in the _layout. cshtml, you could add it in the header.
Besides, you could also try to use jquery-1.10.2.min.js file.
Best regards,
Dillion
Member
59 Points
196 Posts
Re: refering .js files
Dec 28, 2018 05:33 PM|amithashenoy|LINK
Thank you, will try that.
All the Best . Please 'Mark this Post as Answer ' if it helps you.