1

I'm trying to pass a c# 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>
0

4 Answers 4

6

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>
Sign up to request clarification or add additional context in comments.

Comments

2

Javascript can't see your C# source directly - you need to write it into the Javascript source on the server side:

<a  href="#" onclick="return checkAll(<%= count %>);">CheckAll</a>

Comments

1

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().

Comments

0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.