Im having MVC5 application and in the view index.cshtml I need to use some java script code ,currently I put the script code inside the view and its working fine. My question is where should I put this code (from best practice) and how should I refer to it from the view?please provide an example.
相关问题
- Carriage Return (ASCII chr 13) is missing from tex
- MVC-Routing,Why i can not ignore defaults,The matc
- How to store image outside of the website's ro
- 'System.Threading.ThreadAbortException' in
- Request.PathInfo issues and XSS attacks
相关文章
- asp.net HiddenField控件扩展问题
- asp.net HiddenField控件扩展问题
- Asp.Net网站无法写入错误日志,测试站点可以,正是站点不行
- asp.net mvc 重定向到vue hash字符串丢失
- FormsAuthenticationTicket expires too soon
- MVC CDN fallback for Style Bundle
- “Dynamic operations can only be performed in homog
- What is the best way to create a lock from a web a
The approach I've written down below is my way of extracting JavaScript completely from your views.
In HTML5, use the
data
attribute to pass along variables from theModel
. This helps tremendously in porting variables from MVC (your viewmodel) to javascript. This also allows you to keep javaScript stored in separate files as you probably would like in an MVC environment.1.1 Binding c# to HTML
1.2 JS Helper functions to convert data into object literals
Although built on jQuery, I've written 2 small functions which can help porting querystring variables into object literals and back. I use these throughout my js files:
1.3 Convert HTML data into js object literals
With these functions in mind you can pass any query string like variables into an object literal.
1.4 Example: JS modular setup to extend and override object literals
Combined with jQuery's
$.extend()
function you can now override javascript objects in a modular approach (considering all closures a js file/module looks like this):1.5 Initializing a js module
2.1 Adding scripts/css to your views
You have a couple options for attaching scripts to your views/pages/blocks:
ClientResources
(not the best approach in MVC but still doable, allows you to include external files into a partial view -> view in view)2.2.1 baselayout setup for sections
2.2.2 usage partial view
2.3.1 baselayout setup for view in view
2.3.2 usage view in view
2.4.1 BundleConfig setup for scripts
2.4.2 baselayout setup
If you have and HTML 5 template it really doesn't matter where you place the JavaScript code, If you haver a XHTML template you should put your code inside the
<head></head>
tags.Now with the best practices, right now the best practice is to put all your JS code just before your
</body>
closing tag. This way you make sure that your html elements tags have been parse by the browser.When going to a production environment the best is to concatenate all your JS into a single .js file and then have it minify, that way you would have an only small js file that the client browser needs to fetch.
Minify your code
The term minify in frontend code (css/js) stands for a process where you trim all your spaces and line breaks, and also the function scope variables get replace with a shorter name, usually just a vowel.
when minified gets replaced to:
In
MVC4
there was the Bundling and Minification feature that could help you with this. InMVC5
I'm not that sure.Further Reading: http://www.asp.net/mvc/tutorials/mvc-4/bundling-and-minification
You better use Razor
@section
for this.In your Layout:
In your View:
Some would prefer to place
@RenderSection("Scripts")
just before the</body>
tag instead.