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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 21, 20262026-05-21T18:13:27+00:00 2026-05-21T18:13:27+00:00

I am currently building a website in ASP.NET MVC. I am trying to access

  • 0

I am currently building a website in ASP.NET MVC. I am trying to access ViewData in javascript.

Is there any way that I can access a string value using javascript in a View that was stored in ViewData in a Controller Action. ( I am not able to figure out the right syntax ).

I wish to do something like..


var str = ViewData["Text"];

I tried the following:

var str = <%=ViewData[“Text”] %>

but it didn’t work.

Can someone please help.

Thanks.

  • 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-21T18:13:27+00:00Added an answer on May 21, 2026 at 6:13 pm

    Like this (Razor):

    var str = @Html.Raw(Json.Encode(ViewData["Text"]));
    

    or (WebForms), using the JavaScriptSerializer (and after importing theproper namespace to your webform – System.Web.Script.Serialization):

    var str = <%= new JavaScriptSerializer().Serialize(ViewData["Text"])) %>;
    

    And please don’t use ViewData in an ASP.NET MVC application. Use view models and strongly typed views so that your code looks like this:

    var str = <%= new JavaScriptSerializer().Serialize(Model.Text) %>;
    

    This technique is even cooler as now you can JSON serialize the entire view model:

    var model = <%= new JavaScriptSerializer().Serialize(Model) %>;
    var str = model.Text;
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am building an asp.net mvc website, after the user login he can access
I am currently building a custom Membership Provider for my Asp.net MVC website. I
I am building an asp.net apartment rental website. Currently, I do not utilize any
I'm currently building the Admin back-end for a website in ASP.NET MVC. In an
I'm currently building my personal website in ASP.Net MVC and I want two versions
I'm currently building a website (ASP.net c#) and we're looking at creating a live
I am a noob in ASP.NET and building a website currently... I want the
I'm building a website with ASP.NET MVC where one feature displays some points on
I am currently building an ecommerce website using ASP .Net MVC3. At the end
I'm currently building a website .net/c#. I'm looking to see if there is a

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.