I have a .csHtml
-razor file with a javascript function that uses the @Url.Content
C# function inside for the ajax URL.
I want to move that function to a .js
file referenced from my view.
The problem is that javascript doesn't "know" the @
symbol and doesn't parse the the C# code.
Is there a way to reference .js
files from view with "@" symbol?
Well I've just found a razor engine on nuget that does it! Meaning solves
@
syntax!It's name is RazorJS.
The Nuget package
2016 Update:
The package wasn't updated for 5 years, and the project site link is dead. I do not recommend people to use this library anymore.
You could use HTML5
data-*
attributes. Let's suppose that you want to perform some action when some DOM element such as a div is clicked. So:and then in your separate javascript file you could work unobtrusively with the DOM:
This way you could have a pure separation between markup and script without you ever needing any server side tags in your javascript files.
I usually wrap JS needing access to model properties, in functions and then pass the @something in the view. For example
in the view I add function invocation via (just an example):