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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T07:35:08+00:00 2026-05-30T07:35:08+00:00

I’m using Ajax to refresh a partial view and find that the ViewBag contents

  • 0

I’m using Ajax to refresh a partial view and find that the ViewBag contents are not available when the controller is invoked to perform the refresh.

Specifically, I place an item in ViewBag.ItemToMonitor. It is available the first time the partial view is rendered (here I use it in the controller, but I also tried using it in the view). When the partial view is refreshed via Ajax, the ViewBag does not contain anything for ItemToMonitor.

How can I make data available upon refresh?

Here’s a simple example, based on the MVC 3 project template:

Index.cshtml

<div>
    Select an item to monitor:

    <ul>
    <li>@Html.ActionLink("One", "Monitor", new { itemToMonitor = "Item One" })</li>
    <li>@Html.ActionLink("Two", "Monitor", new { itemToMonitor = "Item Two" })</li>
    <li>@Html.ActionLink("Three", "Monitor", new { itemToMonitoritem = "Item Three" })</li>
    </ul>
</div>

Monitor.cshtml

@model string
<script type="text/javascript">
    setInterval(function () {
        $.post('@Url.Action("_MonitorDetails")', function (data) {
            $('#refreshMe').html(data);
        }
    );
    }, 5000);
</script>

<h2>Monitor</h2>

<div>Monitoring @ViewBag.ItemToMonitor</div>
<div id="refreshMe">
@Html.Partial("_MonitorDetails")
</div>

_MonitorDetails.cshtml

@model string

<div style="background-color:Blue; color:White">
This is my partial view to monitor '@Model', updated at @DateTime.Now.ToString("u").
</div>

HomeController.cs

public ActionResult Index()
{
    ViewBag.Message = "Welcome to ASP.NET MVC!";

    return View();
}

public ActionResult Monitor(string itemToMonitor)
{
    ViewBag.ItemToMonitor = itemToMonitor;

    return View((object)itemToMonitor);
}

public ActionResult _MonitorDetails()
{
    string itemToMonitor = ViewBag.ItemToMonitor;
    // itemToMonitor is null.  Why?
    return PartialView((object)itemToMonitor);
}
  • 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-30T07:35:09+00:00Added an answer on May 30, 2026 at 7:35 am

    Don’t think a ViewBag in MVC is like a ViewState in WebForms – it is NOT refilled at every request like the ViewState. You have to set everything in the ViewBag again on the next request.

    So you need another solution, either

    • pass the itemToMonitor string to Monitor in your ajax request
    • save the itemToMonitor in the session and access that in _MonitorDetails
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

That's pretty much it. I'm using Nokogiri to scrape a web page what has
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I have an MVC Razor view @{ ViewBag.Title = Index; var c = (char)146;
I need a function that will clean a strings' special characters. I do NOT
I'm new to using the Perl treebuilder module for HTML parsing and can't figure
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I am reading a book about Javascript and jQuery and using one of the
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I've got a string that has curly quotes in it. I'd like to replace
I'm using v2.0 of ClassTextile.php, with the following call: $testimonial_text = $textile->TextileRestricted($_POST['testimonial']); ... and

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.