0

How do I access the value of a variable set in razor from jQuery? In the code below, I'm expecting "admin" but am getting "object Object" instead. Something fundamental I'm missing.

code

Alert: alert

2 Answers 2

1

Never use alert(object) to try to debug. Use

console.log("_area:", "@_area", $("@_area").length)

Objects always appears as [Object object] when coerced to a string
(either via alert or via "" + obj)

In your case, @_area = "admin" so this will output in the html/javascript as:

$("admin")

which isn't a valid selector, so should be: $("#@_area")

Also note that if you output a jquery object $("bad_selector") - it's still a jquery object, so use $("bad_selector").length to determine if it's working.

You can also view the rendered html to see what your Razor is generating, sometimes this will make the answer obvious.

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

Comments

0

Because $('...') - is JQuery object. Check this out: alert("_area: " + '@_area')

1 Comment

Or just alert('_area: @_area')

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.