Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to organize the JavaScript Code in ASP.NET MVC applications [duplicate]

We are to add some Ajax functions on a view page. Say, click a delete or insert button and an Action will be called to perform a CRUD job behind the scene and then refresh the items list after that operation.

In many existing examples, the javascript code is embeded in the view page within the <script> tag. Since there are usually many functions that one view may need. Say, when insert a record, there will be function to collect the form data, validate the data (this part can be very long), and call the action by $.ajax(). For this reason, we are thinking about place all those javascript into a separate .js file.

So, what is the professional practice in this? Should we place the .js files under the same folder with its Views? Or should be place all of them into the Script folder and reference from there?

Should we create seperate .js files for each individual view or combine all the .js files for different views into a large file?

Also, we love how we can group C# code using #region so as to collapse. Is there anything similar in JavaScript? It just grows lengthy too easily.

Thank you for any helpful advice.

like image 727
Blaise Avatar asked May 01 '12 14:05

Blaise


People also ask

Can we use JavaScript in ASP NET MVC?

JavaScript can be used in asp.net mvc. If you go for Asp.NET Web forms, there is no need to have a detailed understanding of HTML, CSS or JavaScript as it abstracts all these details and provides automatic plumbing.

How can add JavaScript in ASP NET MVC?

Go to Views -> Shared -> _Layout. cshtml file and add the render code. Make sure to register the custom javascript file after the jquery bundle since we are going to use jquery inside our js file. Otherwise we will get a jquery error and also register this before the script RenderSection.

What is layout View in MVC?

The layout view allows you to define a common site template, which can be inherited in multiple views to provide a consistent look and feel in multiple pages of an application. The layout view eliminates duplicate coding and enhances development speed and easy maintenance.


1 Answers

I wouldn't use the Scripts directory for this purpose, because all default JavaScript files (i.e. jQuery and others) are placed here. I generally use the Content directory for my own JavaScript (also CSS and images) files.

You can structure your files in seperate folders like your views (i.e. ~/Content/js/myviewfolder/somename.js).

If you want to structure your JavaScript code: you can seperate independant code into different files and you can let it process to one file. Or you can group it into an anonymous self executing code blocks, you can then collapse those blocks. Apart from that, I am out of ideas. (Personally, I would go for different files (there are ways if building different JavaScript files into one file.)

like image 82
Styxxy Avatar answered Oct 11 '22 07:10

Styxxy