Have to embed javascript code block with
<script type='text/javascript'>
...
</script>
But Razor code won't compile in a .js
file, included from a .cshtml
file.
How to make this work? Or is there any other elegant way to have a similar effect?
Thank you.
Right click on Pages folder then go to Add and then click class. Give the file name as Counter. razor. cs and click Add.
Adding Razor Pages: Annoyances You'll need to right-click on your project in Solution Explorer, select Add Folder and, when the Add Folder dialog is displayed, add a Folder called Pages. Once that Pages folder is created, you can right-click on it and select Add | New Item to display the Add New Item dialog.
Razor is an ASP.NET programming syntax used to create dynamic web pages with the C# or VB.NET programming languages. Razor was in development in June 2010 and was released for Microsoft Visual Studio 2010 in January 2011. Razor is a simple-syntax view engine and was released as part of MVC 3 and the WebMatrix tool set.
When I face this problem sometimes I will provide a function in the .js file that is accessible in the .cshtml file...
// someFile.js
var myFunction = function(options){
// do stuff with options
};
// razorFile.cshtml
<script>
window.myFunction = new myFunction(@model.Stuff);
// If you need a whole model serialized then use...
window.myFunction = new myFunction(@Html.Raw(Json.Encode(model)));
</script>
Not necessarily the BEST option, but it'll do if you have to do it...
The other thing you could maybe try is using data attributes on your html elements like how jQuery.validate.unobtrusive does...
//someFile.js
var options = $("#stuff").data('stuff');
// razorFile.cshtml
<input type="hidden" id="stuff" data-stuff="@model.Stuff" />
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