I need your advice. I’m writing an application ASP.NET MVC + jQuery + jQuery UI that’s work great but there is a lot of jQuery in a page, it’s like 10 years ago with javascript (except jquery is easier). In a view (not all), I have around 50% of HTML (TextBoxFor and other ….) and 50% of jQuery.
Do you have the same feeling ? I do something wrong ?
Thanks,
Yep, but mainly because today’s sites are a lot more interactive than they were 10 years ago, with AJAX etc.
There’s no getting away from client-side code if you want a dynamic interface. The WebForms style (with ASP.NET AJAX) abstracted a lot of it away, but at a cost in terms of flexibility and performance (and a bunch of server-side code clutter instead).
One of the reasons I love MVC is that it forces developers to think with clarity about what they are doing at the server side and client side separately.
If you are having trouble decluttering your MVC Views, consider putting the script in separate include files. However, I understand that often your script is necessarily in the View because it’s got a bunch of inline MVC code with Model references in it e.g.
$.ajax({url: '<%: Url.Action(Model.DeleteCalendarPageName) %>'});Alternatively, if you have a lot of functions on the page, group them into separate script blocks so you can use VS outlining to open and close them for convenience e.g.