0

I have declared this variable in jquery

var enabled = false;

I want to use it in my Html page in my Html hepler parameter like this

@Html.ListMultiple(enabled)

But this code doesn't work since Html doesn't know the variable because it is declared in jQuery.

5
  • if you are with asp.net try with protected string calling Commented Mar 21, 2012 at 13:37
  • 8
    I think you need to learn that basics of DOM manipulation through Java Script. You could also do with learning the difference between a language, JavaScript, and a library, JQuery Commented Mar 21, 2012 at 13:38
  • 5
    ...and the difference between server code and client code. Commented Mar 21, 2012 at 13:40
  • What you are doing isn't possible. When the razor file is being processed your javascript isn't active yet. Those are totaly different things. Commented Mar 21, 2012 at 13:40
  • How can there be variables declared in jQuery? Commented Mar 21, 2012 at 13:40

2 Answers 2

5

I have declared this variable in jquery

No, you haven't. That is plain JavaScript. It would still be JavaScript even if it made use of functions provided by the jQuery library, which it doesn't.

I want to use it in my Html page in my Html hepler parameter like this

That means you want to use it in you server side ASP code. You can't do that.

  1. The ASP generates some text.
  2. The text is sent to the browser.
  3. The ASP finishes running.
  4. The browser parses the text as HTML / JavaScript / CSS / etc
  5. The browser executes the JavaScript

At this stage the JavaScript is:

  • Running on a different machine
  • Running when the ASP has ended

You can't pass data back to the ASP that generated the page.

You could make a new HTTP request (by submitting a form, setting location.href, using Ajax, etc) to send some data to a new invocation of an ASP program.

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

Comments

1

This is not possible. HTML helpers are executed on the server whereas javascript runs on the client, much after the page has been rendered. You could use jQuery to manipulate the DOM tree though.

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.