Im is working on a web application where the main page contains two parts: a constant block that is always displayed, and an information block made up of one of three partial views. Each of the partial views appears as a result of the AJAX request and is loaded only once (after this, the jquery provided switch windows). It works well, but Ive encountered one problem.
The html code for partial representations contains js functions that are also used in the constant block and in the information block. When the page loads, these functions can โseeโ each other and they work, but they cannot find function declarations and warn me about it. I cannot solve the problem by transferring them to an external js file due to the razor syntax that can be found in their code.
What can i do with this?
Thank.
Update:
Finally, I decided to solve the problem separating my js code from the views. So the new question was how to include razor syntax in js files or what is an acceptable alternative. Popular Ive solutions use global variables, data attributes, and the ones I like best - RazorJS library by John Katsiotis.
http://djsolid.net/blog/razorjs---write-razor-inside-your-javascript-files
I hope that it will work stably and make Resharper happy.
Hurrah!
Update:
After 3 years, I remembered this question and decided to update it in accordance with my experience. In fact, now I would prefer not to use additional libraries for this. Especially if you are not the only member of the project team ... It is much better if you are provided with all your libraries, they are supported by the creator and the community and can be easily integrated into your IDE (for example, if you use special syntax), also all the guys from Your team should know how this works. So now I propose to do the following:
- Keep all JS in separate files. Isolate it as much as possible. Provide it with an external API.
- Call API functions from your views.
- Pass all the URLs created by Razor, text messages, constants as a resource parameter.
For example:
js file:
$.api.someInitFunction = function(resources){ ... }
View:
<script> $.api.someInitFunction({ urls: { myAction: '@Url.Action("MyAction", "MyController")' }, messages: { error: '@errorMessage' }, consts: { myConst: @myIntConst } }); </script>
javascript asp.net-mvc razor partial-views
Tomy Jun 19 2018-12-12T00: 00Z
source share