2

how can I use my own c#(MVC5, ASP.NET 4.5.1) variable in jquery ? Something like that:

  $(function () {

        @if (Model != null && Model.Count() > 0){
            int carrier = Model.Count;

        }
        if (location.hash = "#DHLNational") {
            $("#faq-cat-1-sub-0").show(800);
            alert(@carrier);
        }
    });

I get the following error message:

Error 8 "The name 'carrier' does not exist in the current context"

Ok, but if I do it like this:

    $(function () {

        @if (Model != null && Model.Count() > 0){
      int carrier = Model.Count;

      if (location.hash = "#DHLNational") {
          $("#faq-cat-1-sub-0").show(800);
          alert(@carrier);
      }
  }
    });

Visual studio does not understand jquery anymore. Thanks...

4
  • I know in this case I could use "@Model.Count" and it would work, but its more a general question. Commented Jul 28, 2014 at 13:36
  • 1
    Put carrier into a hidden field and select that like any other element? Mixing your Razor and JQuery doesn't feel right. Commented Jul 28, 2014 at 13:38
  • @James I have the same bad feeling, if I would use this(mixing) in a productive environment... Commented Jul 28, 2014 at 13:41
  • int carrier = "<%=Model.Count%>"; ? Commented Jul 28, 2014 at 13:42

2 Answers 2

2

You have declared the variable inside the if statement, so it doesn't exist outside that scope. Declare it outside:

$(function () {

  @{
    int carrier = 0; // needs a value, as the next statement might not set any
    if (Model != null && Model.Count() > 0){
      carrier = Model.Count;
    }
  }
  if (location.hash = "#DHLNational") {
    $("#faq-cat-1-sub-0").show(800);
    alert(@carrier);
  }
});
Sign up to request clarification or add additional context in comments.

4 Comments

Would it be a security problem, if I would use that kind of mixing between jquery and mvc ?
@user254197: No, the server code runs on the server, and is never visible to the browser. If you use View Source in the browser to see the generated code, you will see that none of the server code is included, and all that is left of it is the Javascript code with something like alert(42); in it, where the number comes from the carrier variable. You should naturally be careful about what the code writes out in the page, so that it doesn't "leak" anything that you need to keep secure.
ok, thanks. But if I use this: " for(var currentcarriernumber=0;currentcarriernumber < @carrier;currentcarriernumber++){ if (location.hash = "#DHLNational") { $("#faq-cat-1-sub-"+currentcarriernumber).show(800); alert(currentcarriernumber); } }" - it compiles but visual studio underlines it and says "Syntax error" but only if I hover over the red mark ... is there an error ?(and the "if" doesn't work anymore, it excecutes everything inside the block-it doesn't matter what is written in the URL )))
@user254197: You should use the == comparison operator, not the = assignment operator. It's common for Visual Studio to flag syntax errors in the Javascript when you mix in server code.
0

First you have to declare and cast C# variables to javascript variables like:

for integer variables :

int carrier = parseInt('@Model.Count');

for string variables :

var myJavascriptString = '@Model.MyCSharpString';

Now you can use your javascript variable.

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.