I was told that it is best to put the Javascript code in a separate file to separate the problems, and although the idea resonates with me, I do not find it practical.
It may just be my inexperience, so this is the question.
Here is an example of a clear example where I found that placing code in a view is better than placing it in a separate javascript file.
In my view, I needed to invoke the JQueryUI dialog box and set the title dynamically with the name of my model.
$("#thumbs img").click(function () { var url = $(this).attr("src"); $(".image-popup").attr("src", url); return $("#image-popup").dialog({ modal: true, closeOnEscape: true, minHeight: 384, minWidth: 596, resizable: false, show: { effect: 'slide', duration: 500, direction: 'up' }, hide: { effect: 'slide', duration: 250, direction: 'up' }, title: '@Model.Product.Name' }); });
Note:
title: '@Model.Product.Name'
As you can see, I have access to a strongly typed model if I use Javascript in my view. This is not the case if I use a separate Javascript file.
I am doing it wrong, is there something that I do not see?
If I were to use a separate file, how would it look like a view, since I cannot access the model properties from a Javascript file?
javascript c # asp.net-mvc-3
Only Bolivian Here Apr 05 2018-12-12T00: 00Z
source share