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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T02:27:30+00:00 2026-05-31T02:27:30+00:00

Controller: public ActionResult Edit(string temp) { ViewBag.Time = DateTime.Now.ToString(hh:mm:ss); return PartialView(Edit); } Partial View:

  • 0

Controller:

public ActionResult Edit(string temp)
{
     ViewBag.Time = DateTime.Now.ToString("hh:mm:ss");
     return PartialView("Edit");
}

Partial View:

@using (Ajax.BeginForm("Edit", "Home", new AjaxOptions{UpdateTargetId = "mydiv"}))
    {
        <input type="submit" value="Save" />
    }

Index View (part of contents)

<div id="mydiv">
    <span>The Time is: @ViewBag.Time</span>
</div>
@Html.Partial("Edit")

ClientValidationEnabled and UnobtrusiveJavaScriptEnabled are true
jquery.validate.min.js, jquery.validate.unobtrusive.min.js, jquery.unobtrusive-ajax.min.js, MicrosoftMvcAjax.js and MicrosoftAjax.js are added

At first, the time is shown correctly. When the Save button is clicked for the first time, time disappears and Save button is shown twice and then nothing happens except calling the Action on clicking on both buttons.

  • 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-31T02:27:31+00:00Added an answer on May 31, 2026 at 2:27 am

    You have things kind of backwards. Try this:

    Controller

    public ActionResult Edit(string temp)   
    {  
        ViewBag.Time = DateTime.Now.ToString("hh:mm:ss");  
        return PartialView("Edit");   
    }
    

    Index View

    @using (Ajax.BeginForm("Edit", "Home", new AjaxOptions{UpdateTargetId = "mydiv"}))     
        {     
            <input type="submit" value="Save" />     
        }            
    @Html.Action("Edit") 
    

    Partial View (Edit)

    <div id="mydiv">
        <span>The Time is: @ViewBag.Time</span> 
    </div>
    

    The ViewBag is only accessable at runtime (when the page initially loads) so this means if you fetch data via ajax, the viewbag in the controller action is only accessable to the partial view of that controller action (and not index.cshtml which called the action via ajax). In short (tl;dr) to be able to use the viewbag which you set in Edit action, you need to use it in the returning partialview. (and not anywhere else, because the content isnt re-rendered by the razor engine)

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

Sidebar

Related Questions

I Have following code: Controller: public ActionResult Step1() { return View(); } [AcceptVerbs(HttpVerbs.Post)] public
In my controller I have the following (code omitted for brevity): public ActionResult Edit(string
I have the following controller: public ActionResult Search(string Name, int? Friend, int? Page) It
The controller is: public class HomeController : Controller { public ActionResult LogOn() { return
I have the following code in my controller: public ActionResult Details(int id) { var
I write a controller like below: public class AccountController : Controller { public ActionResult
I have an overloaded action in my Controller: public ActionResult AssignList(int id) { ...
I have an action like this: public class News : System.Web.Mvc.Controller { public ActionResult
I have a fairly standard .Net MVC Controller method: public ActionResult Add(Customer cust) {
I have multiple controller actions that takes an id public ActionResult Get(int? id) {

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.