访问 javascript 文件中的模型属性?
是否可以访问外部 Javascript 文件中的模型属性?
“somescript.js”文件中,
var currency = '@Model.Currency';
alert(currency);
例如,在我的视图上的
<script src="../../Scripts/somescript.js" type="text/javascript">
这似乎不起作用,但是如果我将 javascript 直接放入脚本标记内的视图中,那么它会起作用吗?这意味着必须始终将代码放在页面中,而不是像这样加载外部脚本文件:
@model MyModel;
<script lang=, type=>
var currency = '@Model.Currency';
alert(currency);
</script>
有什么办法解决这个问题吗?
Is it possible to access a Model property in an external Javascript file?
e.g. In "somescript.js" file
var currency = '@Model.Currency';
alert(currency);
On my View
<script src="../../Scripts/somescript.js" type="text/javascript">
This doesn't appear to work, however if I put the javascript directly into the view inside script tags then it does work? This means having to put the code in the page all the time instead of loading the external script file like this:
@model MyModel;
<script lang=, type=>
var currency = '@Model.Currency';
alert(currency);
</script>
Is there any way around this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
我使用数据属性和 jQuery 解决了这个问题。它使得代码非常可读,并且不需要部分视图或通过 ViewEngine 运行静态 javascript。 JavaScript 文件是完全静态的,并且会正常缓存。
Index.cshtml:
Index.js:
_Layout.cshtml(可选,但好习惯):
I tackled this problem using data attributes, along with jQuery. It makes for very readable code, and without the need of partial views or running static javascript through a ViewEngine. The JavaScript file is entirely static and will be cached normally.
Index.cshtml:
Index.js:
_Layout.cshtml (optional, but good habit):
无法在 JS 文件中实现 MVC/Razor 代码。
您应该在 HTML 中(在 .cshtml 文件中)设置变量数据,这在概念上是可以的,并且不会违反关注点分离(服务器生成的 HTML 与客户端脚本代码),因为如果您考虑一下,这些变量值是服务器问题。
看看这个(部分但很好)的解决方法: 在 MVC 框架中的 Javascript 文件中使用内联 C#
There is no way to implement MVC / Razor code in JS files.
You should set variable data in your HTML (in the .cshtml files), and this is conceptually OK and does not violate separation of concerns (Server-generated HTML vs. client script code) because if you think about it, these variable values are a server concern.
Take a look at this (partial but nice) workaround: Using Inline C# inside Javascript File in MVC Framework
您可以做的是将剃刀标签作为变量传递。
在剃刀文件中>
在JS文件中>
What you could do is passing the razor tags in as a variable.
In razor File>
in JS file >
我所做的是使用方法调用模式创建一个 js 对象,然后您可以从外部 js 文件调用它。由于js使用全局变量,我将其封装起来,以确保不会与其他js库发生冲突。
例子:
在外部页面的 Now视图中,
您可以获取具有受保护范围的数据,以确保它不会与 js 中的其他全局变量冲突。
您还可以将其包装在外部文件的模块内,以使其更加防冲突。
例子:
What i did was create a js object using the Method Invocation pattern, then you can call it from the external js file. As js uses global variables, i encapsulate it to ensure no conflicts from other js libraries.
Example:
In the view
Now inside of the external page you can then get the data with a protected scope to ensure that it does not conflict with other global variables in js.
Also you can wrap it inside of a Module in the external file to even make it more clash proof.
Example:
尝试 JavaScriptModel ( http://jsm.codeplex.com ):
只需将以下代码添加到您的控制器操作中:
现在您可以在 JavaScript 中访问变量“Currency”。
如果该变量应该在孔位上可用,请将其放入过滤器中。可以在文档中找到如何从过滤器使用 JavaScriptModel 的示例。
Try JavaScriptModel ( http://jsm.codeplex.com ):
Just add the following code to your controller action:
Now you can access the variable "Currency" in JavaScript.
If this variable should be available on the hole site, put it in a filter. An example how to use JavaScriptModel from a filter can be found in the documentation.
你总是可以尝试 RazorJs。它几乎解决了无法在 js 文件中使用模型的问题 RazorJs
You could always try RazorJs. It's pretty much solves not being able to use a model in your js files RazorJs
我遇到了同样的问题,我这样做了:
查看。
外部js。
I had the same problem and I did this:
View.
External js.