My .cshtml file in Visual Studio looks like this:
Notice how createTabStrip
can collapse, but createTeachersTab
cannot. Why is this?
EDIT: It seems to have something to do with the razor syntax. I took out all of the @
signs and createTeachersTab
was able to collapse.
To expand on my comment.
You generally do not want to define functions in your Razor views. Instead, define them and import them from an external JavaScript file. If you need info from C# in JavaScript, you could create a global config object in JavaScript in a Razor partial, then render that partial.
function_lib.js
function createTeachersTab() {
...
read: {
url: config.teachers.newTabUrl
}
...
}
Views/Shared/_JavaScriptConfig.cshtml
This would be rendered as a Partial in the <head>
of the HTML document.
<script type="text/javascript">
var config = {
teachers: {
newTabURL: '@Url.Action("Teachers", "Users")'
}
};
</script>
Then anywhere else in your JavaScript, you can reference these settings via the global config
JavaScript variable.
config.teachers.newTabUrl
Edit: I also completely recognize that this does not solve the code collapsing problem in Visual Studio, which appears to be a parsing bug on their end. The real solution is "Don't define JavaScript functions in Razor views" as this is considered bad practice.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With