[英]pass a c# variable to javascript function
I'm trying to pass ac# variable as an argument to a java-script function like 我试图将ac#变量作为参数传递给Java脚本函数,例如
<%var count=model.SomeMode.Count();%>
when i pass it to my java script function "checkAll(count)" it does not fire but without the argument its workin fine 当我将其传递给我的Java脚本函数“ checkAll(count)”时,它不会触发,但没有参数,则可以正常工作
<a href="#" onclick="return checkAll(count);">CheckAll</a>
Remember the page gets created however you wish. 请记住,页面是根据您的意愿创建的。 So, you could do something like:
因此,您可以执行以下操作:
<script type="text/javascript">
var count = <%=model.SomeMode.Count(); %>;
</script>
You could apply the same logic in method calls, so you could do: 您可以在方法调用中应用相同的逻辑,所以您可以执行以下操作:
<a href="#" onclick="return checkAll(<%=model.SomeMode.Count(); %>);">CheckAll</a>
Javascript无法直接看到您的C#源代码-您需要将其写入服务器端的Javascript源代码中:
<a href="#" onclick="return checkAll(<%= count %>);">CheckAll</a>
It should look like this: 它看起来应该像这样:
<script type="text/javascript">
var count=<%=model.SomeMode.Count()%>;
</script>
Currently you're declaring your variable in C#, not outputting it in the page as a JavaScript one. 当前,您正在用C#声明变量,而不是在页面中将其作为JavaScript输出。 Instead, you want to declare
var count
literally in the page and have it set to the output of model.SomeMode.Count()
. 相反,您想在页面上
model.SomeMode.Count()
地声明var count
并将其设置为model.SomeMode.Count()
的输出。
This might be good for your solution: 这可能对您的解决方案有利:
<a href="#" onclick='return checkAll(<%=count%);'>CheckAll</a>
But there is an other way of doing this instead of conventional way: 但是还有另一种方法可以代替传统方法:
Sharing Variables Between JavaScript and C# by Fredrik Kalseth 在JavaScript和C#之间共享变量 ,作者Fredrik Kalseth
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.