0

There is an error when trying to include the javascript variable "selectedValue" in the c# function being called. SelectedValue has red underline error inside the GetTotalLicensesCount.

function drawExpireGauge() {

    var selectedValue = "30days";

        var data = google.visualization.arrayToDataTable([
              ['Label', 'Value'],
              ['Expiring', <%=GetTotalLicensesCount(null, null, null, selectedValue, selectedValue, null, null)%>],
              ['New', <%=GetTotalLicensesCount(null, null, null, null, null, null, selectedValue)%>],
              ['Not Used', 0]
        ]);
}

if "30days" is simply placed inside the GetTotalLicensesCount function instead of the variable, it works.

I did due-diligence and searched around the forum for a similar question but couldn't find any, please let me know how the question should be changed or if there is a reasonable duplicate that answers the question.

1
  • var selectedValue = "30days"; is being executed as javascript (in the users browser) not in Razor (on the server before being sent to the user). Wrap it in @{ and } to have it evaluated on the server. However, if 30days is going to be taken from input from the page (for example whatever a user clicks/types in), you'll need to send a request to the server to properly evaluate it as Keith mentions Commented Jan 17, 2017 at 0:10

1 Answer 1

1

The C# code doesn't get executed as part of the js execution.

The injected C# code you have is part of a template that will generate the js code that will eventually get executed on the client. Therefore the C# code will not have access to "selectedValue".

so, C# code, gets run on the server, helps generate the HTTP response sent back to the client. JS code, gets run on client once it has the response.

You have a couple of options, Ajax back to your server to get the data, or provide all the data to js so you can write a js version of GetTotalLicensesCount.

The choice of which method depends on :-

1) how much data there is ( small, do it in js, large, ajax)

2) if any of the data needed for the calculation is sensitive and you don't want the user to see it. (always use ajax to avoid exposing sensitive data)

Sign up to request clarification or add additional context in comments.

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.