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 7893541
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T07:09:31+00:00 2026-06-03T07:09:31+00:00

Is it possible to do the following from a javascript file in an MVC

  • 0

Is it possible to do the following from a javascript file in an MVC application?

$(function(){
   alert(@ViewBag.someValue);
}

Currently it throws the error:

reference to undefined XML name @ViewBag

  • 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-03T07:09:32+00:00Added an answer on June 3, 2026 at 7:09 am

    I don’t believe there’s currently any way to do this. The Razor engine does not parse Javascript files, only Razor views. However, you can accomplish what you want by setting the variables inside your Razor view:

    <script>
      var someStringValue = '@(ViewBag.someStringValue)';
      var someNumericValue = @(ViewBag.someNumericValue);
    </script>
    <!-- "someStringValue" and "someNumericValue" will be available in script -->
    <script src="js/myscript.js"></script>
    

    As Joe points out in the comments, the string value above will break if there’s a single quote in it. If you want to make this completely iron-clad, you’ll have to replace all single quotes with escaped single quotes. The problem there is that all of the sudden slashes become an issue. For example, if your string is “foo \' bar“, and you replace the single quote, what will come out is “foo \\' bar“, and you’re right back to the same problem. (This is the age old difficulty of chained encoding.) The best way to handle this is to treat backslashes and quotes as special and make sure they’re all escaped:

      @{
          var safeStringValue = ViewBag.someStringValue
              .Replace("\\", "\\\\")
              .Replace("'", "\\'");
      }
      var someStringValue = '@(safeStringValue)';
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Following this question, it seems that it is possible to open a file from
Possible Duplicate: Uploadify: show error message from HTTP response I am developing the application
Possible Duplicate: What does this mean? (function (x,y)){…}){a,b); in JavaScript I have the following
Possible Duplicate: Writing an SQL query to SELECT item from the following table I
Is this possible to do the following in ExpressionEngine: (code taken from here )
I have the following PHP code which works out the possible combinations from a
I am wondering if the following is possible in MVC 3 I basically have
Is it possible to call Java (GWT) methods from Javascript? It is also unclear
Is it possible to subclass and inherit from javascript Arrays? I'd like to have
Possible Duplicate: How do I test for an empty Javascript object from JSON? Is

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.