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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T20:25:50+00:00 2026-05-26T20:25:50+00:00

I am attempting to manipulate an asp.net MVC2 view variable in Jquery… (function ()

  • 0

I am attempting to manipulate an asp.net MVC2 view variable in Jquery…

(function () {
    debugger;
    var $a = 10;
    var $b = '<%:toggle %>';
    var $c = 12;
});

As you may have guessed, I am attempting to manipulate the toggle variable that I have initiated in an ASP.net MVC2 view in Jquery.

What I did above doesn’t seem to work at all. The page that I attempt to load, blows up every time I try and open it. Any ideas why that might be? Note… Even if I get rid of the ‘ ‘ (apostrophes) my page still blows up. I also attempted to make the variable in question public. That doesn’t seem to work either.

UPDATE EXPECTED vs WHAT I GET
I don’t expect to get this…

my page with that value set to the variable in the view that I described earlier

Here is what I do expect
my page when I set the variable equal to 11 or something trivial

I am having a hard time getting the error message that is being sent to the page. I have to figure out where its being generated and try and chase it down from there. My coworker set up some kind of Javascript error routing scenario for this particular page. And I am having a little trouble deciphering what is going on. If I knew the error I would probably be in good shape.

UPDATE 2 Just figured out the error

TemplateInfo.aspx(371): error CS0103: The name ‘toggle’ does not exist in the current context…

I am guessing that variable has to be somehow made public or put into scope somehow. How can. To my knowledge you can’t create public variables with MVC2? Can you?

  • 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-05-26T20:25:51+00:00Added an answer on May 26, 2026 at 8:25 pm

    It looks like the toggle variable that you are trying to use is nowhere defined in your view.

    In ASP.NET MVC controller actions normally pass view models to views. Those view models contain properties that will be used by the view. So as always in an ASP.NET MVC application you start by defining a view model:

    public class MyViewModel
    {
        public bool IsToggle { get; set; }
    }
    

    then you write a controller action which instantiates this view model and passes it to the view:

    public ActionResult Index()
    {
        var model = new MyViewModel
        {
            IsToggle = true
        };
        return View(model);
    }
    

    and finally you write a strongly typed view to this view model where you can use its properties:

    <%@ Page 
        Language="C#" 
        MasterPageFile="~/Views/Shared/Site.Master" 
        Inherits="System.Web.Mvc.ViewPage<AppName.Models.MyViewModel>" %>
    
    <asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="server">
    
        <script type="text/javascript">
        (function () {
            debugger;
            var $a = 10;
            var $b = <%= Model.IsToggle %>;
            var $c = 12;
        });    
        </script>
    
    </asp:Content>
    

    Now let’s suppose that your view model contains some properties that are of type string or even other complex objects. In this case you will need to properly encode them. And the best way to ensure that you are properly encoding values that you are passing to javascript is to use javascript literals (JSON notation):

    <script type="text/javascript">
    (function () {
        debugger;
        // we JSON serialize the entire model:
        var model = <%= new JavaScriptSerializer().Serialize(Model) %>;
    
        // Now you can manipulate individual model properties:
        alert(model.SomeProperty);
    });    
    </script>
    

    Note that the JavaScriptSerializer class is defined in the System.Web.Extensions assembly in the System.Web.Script.Serialization namespace so make sure to bring those into scope.

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

Sidebar

Related Questions

I was initially attempting visualize a 4 parameter function with Plot3D and Manipulate sliders
Attempting to generics-ify some legacy code, I'm stuck. I have a ParentObject which wraps
While attempting to build a website, i have gone through many online tutorials. Thanks
I am attempting to manipulate a DOM tree using lxml 's etree module. One
I have an SSIS package I am developing. I am attempting to write data
I'm attempting to display a LargeIcon view in a listview control, however the images
I'm looking to write a jQuery function that will change the value of other
About a year ago I wrote a jQuery-inspired library which allowed you to manipulate
I am attempting to manipulate an Adobe Reader plist file on OS X to
Attempting to insert an escape character into a table results in a warning. For

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.