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

  • Home
  • SEARCH
  • 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 6255351
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T14:20:58+00:00 2026-05-24T14:20:58+00:00

javascript I am passing the value 3 from a Controller into a ViewData variable

  • 0

javascript
I am passing the value “3” from a Controller into a ViewData variable to be displayed via the HTML.

I have verified that right before the ActionResult is returned, that the value “3” is in the variable (see below code):

public ActionResult Index()
.
.
.
ViewData["ErrCode"] = "3";
                    return RedirectToAction("Index", "Home");

However, the string, when rendered, is always rendered as an empty string (see below):

        $().customAlert();
$(document).ready(function () {
'' = "3";
var strErrCode = '';
alert('Hello!');
});

The above rendered code is from the following code in the tag of the _Layout.cshtml Shared View file. Note that I am even forcing the value “3” into the “ErrCode” variable and the script gets rendered as above.

    $().customAlert();
    $(document).ready(function () {
             '@ViewData["ErrCode"]' = "3";
        var strErrCode = '@ViewData["ErrCode"]';
        alert('Hello!');
    });

I must be using the ViewData incorrectly in the js. How can I accomplish this simple feat?

  • 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-24T14:20:58+00:00Added an answer on May 24, 2026 at 2:20 pm

    You are calling RedirectToAction. This has the effect of returning a 302 http status code pointing you at the URL of the Home action on the Index controller.

    At that point, you lose whatever was in ViewData. You need to pass the value as part of the URL, for example via the query string:

    return RedirectToAction("Index", "Home", new { ErrCode = 3 });
    

    Update: You can also use TempData instead of ViewData. This is a collection that persists data until the next request (and the next request only).

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

Sidebar

Related Questions

When passing boolean value from controller to the view using ViewData, how do I
i m suffering with Passing values from javascript function to controller in cakephp and
I have tried to use the following code to passing value back from the
I'm passing an array from my Rails app to Javascript via a js.erb template.
I have an html/jsp form that I am serializing(using jquery) and passing to a
I need value to be passing from ASP.NET page to JavaScript and then to
I'm trying to solve the problem of passing a 2-dimensional table into JavaScript AJAX
I'm passing the ABSPATH value from a wordpress theme options page to an external
I'm trying to call a Flash function from javascript passing complex data types as
I have a javascript function that takes in a parameter that is given by

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.