简体   繁体   中英

pass a c# variable to javascript function

I'm trying to pass ac# variable as an argument to a java-script function like

<%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

<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. Instead, you want to declare var count literally in the page and have it set to the output of 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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM