0

How can I take a boolean from the Model in my cshtml file and pass it to the javascript constructor to be used during the running of the javascript? The code I have currently shows the variable as "undefined" when debugging.

So, in cardScanner.cshtml I have a model with the bool OcrEnabled, so I am trying to pass @Model.OcrEnabled to the javascript constructor by doing this:

window.dls = new CardScanner("#license", @Model.OcrEnabled.ToString().ToLower());

Then, in the javascript file scanner.js I have the constructor,

CardScanner = function (scope, ocrEnabled) {
    this.scope = $(scope);
    this.ocrEnabled == ocrEnabled;
};

and then the function where I am trying to use the variable

if (this.ocrEnabled == false) 

At this moment in the debugger "this.ocrEnabled" reads undefined.

1
  • 1
    What does the generated HTML look like? Commented Mar 27, 2020 at 22:19

1 Answer 1

1

Are you sure that you wanted to compare unasigned value in your constructor?

this.ocrEnabled == ocrEnabled;

That line seems like typical assigning values in constructor, so I would write instead:

this.ocrEnabled = ocrEnabled;
Sign up to request clarification or add additional context in comments.

1 Comment

Changing that seems to have solved my issue, I'm glad it was something so simple. I've been staring at this code for way too long and fried my brain, thanks for your help!

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.