• Stack Overflow Public questions & answers
  • Stack Overflow for Teams Where developers & technologists share private knowledge with coworkers
  • Talent Build your employer brand
  • Advertising Reach developers & technologists worldwide
  • Labs The future of collective knowledge sharing
  • About the company

Collectives™ on Stack Overflow

Find centralized, trusted content and collaborate around the technologies you use most.

Q&A for work

Connect and share knowledge within a single location that is structured and easy to search.

Get early access and see previews of new features.

How to pass variables from Razor to JavaScript?

I have one C# variable "value" that I want to pass into JavaScript Chartjs data object. It renders the chart but does not include the two @p values. See code source below:

cshtml file:

javascript file:

How can I write it so that it works?

user3062459's user avatar

  • 1 Possible duplicate of Using Razor within JavaScript –  Samvel Petrosov May 23, 2017 at 18:50
  • 1 Possible duplicate of passing server side mvc variables to javascript –  Dayan May 23, 2017 at 18:59
  • 1 Sorry, can you edit your answer for two variables @p1 and @p2? –  user3062459 May 23, 2017 at 19:08
  • 1 Add p as a data attribute to lineChart element, e,g, data-p attrribute and get its value using jQuery : $('#lineChart').data('p') –  rraszewski May 23, 2017 at 19:09

3 Answers 3

and use p1, p2.

In this case you don't have to use hidden html fields and you can expand your code to use more field like

Kumar Nitesh's user avatar

  • 1 One thing to add in is that any "@" that are part of the static javascript code ( as used in JSON+LD for schema.org) would need to be escaped as { "@@Key": "Value" } –  Mad Myche May 23, 2017 at 19:31

Something like below

JavaScript:

Shan's user avatar

Use a hidden field which will hold the value and then read the hidden field's value from JavaScript.

George Findulov's user avatar

Your Answer

Sign up or log in, post as a guest.

Required, but never shown

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct .

Not the answer you're looking for? Browse other questions tagged javascript c# charts or ask your own question .

  • The Overflow Blog
  • Like Python++ for AI developers
  • Being creative with math: The immersive artist who traded a sketchpad for a...
  • Featured on Meta
  • Alpha test for short survey in banner ad slots starting on week of September...
  • What should be next for community events?
  • OverflowAI Search is now available for alpha testing (September 13, 2023)
  • Temporary policy: Generative AI (e.g., ChatGPT) is banned
  • Expanding Discussions: Let's talk about curation
  • Update on Collectives and Discussions

Hot Network Questions

  • Delete list elements above a certain threshold
  • Word Implying Lack of Caring Because You're Already (Wrong/Losing/a Failure/Behind/Insignificant)
  • What is mode borrowing?
  • Should I cite pubpeer comments?
  • Why would people join a bloc party?
  • How do I properly address and handle work burnout?
  • How to check if at least one ordering of the given row matches one of the rows of a table?
  • How to define an Ideal in the language of rings
  • Are "need to call objects in parent object" and "avoid circular dependency" reasons to avoid "Tell, don't ask"?
  • What is this unusual syntax for the Commodore SYS command?
  • How to set up a virtual payment card on a phone that a child can use abroad (Poland) to withdraw cash and pay in shops?
  • Why does English use the French "sans" for sans serif?
  • Can the collapse of the wave function be modelled as a quantum system on its own?
  • Documentation for programmers regarding expl3
  • Do you get the benefits of Oversized Weapons with Path of Giant?
  • Can you "open" RAW camera files?
  • Low consumption resistor pair
  • Make custard firmer
  • Fubini's theorem and reparameterisation
  • Using 15 amp smart switch requiring 14awg wires for lighting/fans on 20 amp circuit
  • Where can I sell large quantities of guns the easiest?
  • Super soldier thief hired to steal vase of incredible beauty
  • Unphysical voltage in SPICE simulation
  • Book of short stories I read as a kid; one story about a starving girl, one about a boy who stays forever young

assign javascript variable to c# variable razor

Your privacy

By clicking “Accept all cookies”, you agree Stack Exchange can store cookies on your device and disclose information in accordance with our Cookie Policy .

IMAGES

  1. c#

    assign javascript variable to c# variable razor

  2. c#

    assign javascript variable to c# variable razor

  3. 38 How To Use A Variable In Javascript

    assign javascript variable to c# variable razor

  4. Vigile pub indurire javascript inject variable into string

    assign javascript variable to c# variable razor

  5. c#

    assign javascript variable to c# variable razor

  6. c#

    assign javascript variable to c# variable razor

VIDEO

  1. JavaScript vs C#

  2. javascript variable part-03 bangla

  3. JavaScript var let const Variable Keyword

  4. Mastering JavaScript Variable Declaration: let, var, and const

  5. Manual razor, men's shaving tools, six speed variable speed razor Recommended #Trend #shaving

  6. Variable part 2

COMMENTS

  1. c#

    c# - How to pass a value to razor variable from javascript variable? - Stack Overflow How to pass a value to razor variable from javascript variable? Ask Question Asked 8 years, 7 months ago Modified 2 years, 6 months ago Viewed 207k times 62

  2. asp.net mvc 3

    Is it possible to assign javascript variable to c# variable in mvc razor view? e.g. <script>@cSharpVar=javascriptVar;</script> Also, is it possible to pass javascript variable to c# method? e.g. @ {var test=Utility.MethodName (javascriptVar,cSharpVar)} asp.net-mvc-3 Share Follow asked May 6, 2011 at 22:39 Nil Pun 17k 39 172 294 Add a comment

  3. c#

    1 Answer Sorted by: 22 They are treated as variable and since you have not defined them you get the said error. You need to wrap in quotes to be treated as string. var SessAnnouncement = "@strAnnouncement"; A better approach would be to use JSON.Encode () method

  4. asp.net

    Razor variable into Javascript Ask Question Asked 6 years, 2 months ago Modified 6 years, 2 months ago Viewed 8k times 4 I am trying to set a JavaScript variable from a model variable using Razor, and can't figure out what is wrong. In the CSHTML page, I've tried the following ways:

  5. c#

    var chart = document.getElementById ("lineChart"); var data = { labels: [2012, 2013, 2014, 2015, 2016, 2017], datasets: [ { label: "My Chart Label", fill: false, lineTension: 0.1, data: [' (@p1)', ' (@p2)', 50, 48, 47, 52] } ] }; var lineChart = new Chart (chart, { type: 'line', data: data } ); How can I write it so that it works?