Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8688851
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T23:28:55+00:00 2026-06-12T23:28:55+00:00

If I wanted to set some javascript variables from my model would that be

  • 0

If I wanted to set some javascript variables from my model would that be a safe practice? For example:

<script type="text/javascript">
    var myVariable = '@Model.myVar';
</script>

Is this an ok practice? I cant think of a better way to really do it, besides maybe doing hidden input, but that seems like too much work compared to this. I just dont know if this is vulnerable or if there’s something im overlooking by doing this.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-12T23:28:57+00:00Added an answer on June 12, 2026 at 11:28 pm

    I think there are a couple of issues with this. Fundamentally, you are tightly coupling your javascript to your page or view. I would consider myself a solid .NET / C# / MVC developer but a beginner with javascript and this is exactly the type of design that I pursued up until recently. In the past I would in-lining all kinds of javascript within my views and making heavy use of Razor. In the end it works but it leads to a very tight coupling of your markup and script and ultimately produces a difficult environment to maintain.

    A much better approach would be to instead in-line calls to javascript functions. The difference is subtle but it will enforce the separation of these two areas.

    <script type="text/javascript">
        MyFunction('@Model.myVar');
    </script>
    

    You can also in-line values in hidden input fields, spans etc via Razor and expose them there to your javascript.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I wanted to set some handler for all the unexpected exceptions that I might
I wanted to use a hashtable as set of some objects for a graphic
I wanted to set a value of BrowsableAttribute for some of MyClass instance's properties
I wanted to set the transactionManager based on some input criteria and hence I
I am looking for some native JavaScript, or jQuery plugin, that meets the following
What would you suggest, if I wanted to blend text messages in and out
I'm using Sinatra, and I wanted to set up some of the convenience rake
I wanted to set up a simple data communication between two C# applications, and
I set up a new webapp Maven project and wanted to test it with
I wanted to be able to set the keyEquivalent of an NSMenuItem based on

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.