Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
467 views
in Technique[技术] by (71.8m points)

asp.net mvc - Is it OK to put JavaScript in Partial Views

I’m working on the web app where the main page contains two parts: the constant block which is always visible and the info-block made up by one of 3 partial views. Each of the partial views appears as a result of AJAX request and is loaded just once (after that switching windows is provided by jquery). It works well but I’ve faced with one problem.

The html-code of partial views contains js functions that are used in the constant block and in the info-block as well. When the page is loaded, these functions can “see” each other and it works but resharper can’t find function declarations and warn me about this. I can’t solve the problem by transferring them into external js file because of razor syntax which can be found in their code.

What can I do with this?

Thanks.

Update:

Finally I’ve decided to solve the problem separating my js code from views. So the new question was how to include razor syntax into js files or what is the acceptable alternative. The popular solutions I’ve found are using global variables, data attributes and the one I like more – RazorJS library by John Katsiotis.

http://djsolid.net/blog/razorjs---write-razor-inside-your-javascript-files

I hope it’s going to work stable and make Resharper happy.

Cheers!

Update:

After 3 years I recalled this question and decided to update it according to my experience. In fact now I would rather not recommend using additional libraries for that. Especially if you are not the only member in the project team… It is much better if you are ensured in all of your libraries, they are supported by creator and community and can be easily integrated into your IDE (if use special syntax for example). Also all the guys from your team should be aware of how does it work. So now I would suggest doing the next things:

  1. Hold all the JS in separate files. Isolate it as much as you can. Provide the external API for it.
  2. Call the API functions from your Views.
  3. Pass all the Razor generated URLs, text messages, constants as 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>
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

If Resharper warns you it's not a big deal ^_^

But if I were you I wouldn't put JavaScript in the partial view at all.

Because the partial view could be inserted in one page many times then you'll get an issues with your JavaScripts.

And for your case if you couldn't separate the JavaScript to JS file then just create another PartialView and put these scripts in it and just render it in your main page.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...