MVC:在没有任何 HTML 控件的情况下在 View 中调用 Javascript 函数
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/14204046/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
MVC: Calling Javascript function in View without any HTML control
提问by jpo
I have a view in which I do some condition check when the page load.
我有一个视图,在该视图中我会在页面加载时进行一些条件检查。
if(model.count()>0){
}
I then want to call a javascript function if this condition is satisfied. How can I do this please?
如果满足此条件,我然后想调用一个 javascript 函数。请问我该怎么做?
I know how to do that within an html control but can this be done without any control?
我知道如何在 html 控件中执行此操作,但是可以在没有任何控件的情况下完成此操作吗?
Or how can i do this check in the javascript, thus having to refer to the view's model?
或者我如何在 javascript 中进行此检查,从而必须参考视图的模型?
EDIT
编辑
This is what I have now, but the function is not recognized: The name myfunction does not exist in the current context
这就是我现在所拥有的,但无法识别该功能: The name myfunction does not exist in the current context
<script>
$(document).ready(function () {
@if (Model.Count() > 0)
{
myfunction(Model.parameter);
}
});
</script>
EDIT
编辑
I changed that to
我把它改成
@if (Model.Count() > 0)
{
<script type="text/javascript">
$(document).ready(function () {
myfunction(@Model.parameter);
});
</script>
}
But not working. Even if I try something like alert("test" + @Model.parameter)
within the .ready(function()), it does not work.
但不工作。即使我alert("test" + @Model.parameter)
在 .ready(function()) 中尝试类似的东西,它也不起作用。
Alternatively, I will have to write the code in the view. I tried eliminating the function and doing it in the view but i will need a way of setting the id property of html element to a variable. something like var variable = Model.param1
and then having <div id=variable></div>
. But how can I set the id property to a variable?
或者,我将不得不在视图中编写代码。我尝试消除该函数并在视图中执行此操作,但我需要一种将 html 元素的 id 属性设置为变量的方法。类似的东西var variable = Model.param1
,然后有<div id=variable></div>
。但是如何将 id 属性设置为变量?
回答by bPratik
well you can simply add the javascript in between the braces. So if you are using Razor
好吧,您可以简单地在大括号之间添加 javascript。所以如果你正在使用Razor
@if(model.count()>0){
<script>
// Javascript code here
</script>
}
What this will do is add the javascript in the page which will then be executed when the browser renders the page.
这将做的是在页面中添加 javascript,然后在浏览器呈现页面时执行该 javascript。
A good practice would be to execute this code when the whole document is ready. If you are using jQuery, you can do this using $(document).ready(function(){ /*Code goes here*/ });
一个好的做法是在整个文档准备就绪时执行此代码。如果您使用的是 jQuery,则可以使用$(document).ready(function(){ /*Code goes here*/ });
回答by acuntex
With JavaScriptModel ( http://jsm.codeplex.com) you could write the following code in your controller action:
使用 JavaScriptModel ( http://jsm.codeplex.com),您可以在控制器操作中编写以下代码:
if (list.Count > 0)
{
this.AddJavaScriptFunction("myjavascriptfunction", parameter);
}
You would not need to modify your view and you won't have any inline obstrusive javascript.
您不需要修改您的视图,并且您不会有任何内联干扰性 javascript。
回答by Chamila Chulatunga
You could use a non-rendering html element (e.g a hidden Div) and write out the value of model.count
, or even true/false to signify whether model.count > 0
into that hidden element. Then in your JavaScript you can obtain the value from the hidden element and run the condition.
您可以使用非渲染 html 元素(例如隐藏的 Div)并写出 的值model.count
,甚至是 true/false 来表示是否model.count > 0
进入该隐藏元素。然后在您的 JavaScript 中,您可以从隐藏元素中获取值并运行条件。
Obviously this isn't appropriate if there are security implications with applying the condition. If this is the case then 'bPratik's suggestion would be more appropriate.
显然,如果应用该条件存在安全隐患,这是不合适的。如果是这种情况,那么 'bPratik 的建议会更合适。
回答by Forty-Two
Try putting the if statement outside of the script tag like this
尝试将 if 语句放在脚本标记之外,如下所示
@if (Model.Count() > 0)
{
<script type="text/javascript">
$(document).ready(function () {
myfunction(@Model.parameter);
});
</script>
}