0

I have an action method, where I set my temp data variable

[HttpGet]
public ActionResult Index1()
{
    if (TempData["Name"] == null)
    {
        TempData["Name"] = "Vinay";
    }
    return View();
}

I have also corresponding view where I use keep method to persist the data into tempdata

@using (Html.BeginForm("Index1", "Employee", FormMethod.Post, new { @class = "navbar-right" }))
{
    <h1>
        <h1>@TempData["Name"]</h1>
        @{ TempData.Keep("Name"); }
    </h1>
    <input type="submit" />
}

First time it working correctly but when I refresh the page again by pressing f5 then TempData["Name"] becomes null. why?

  • Your lien of code `@TempData["Name"]` reads the value and immediately deletes it from `Session` –  Sep 19 '16 at 12:40
  • @StephenMuecke :- at view side, I used tempdata.keep method to persist the data, so I believe that value should not delete immediately. – Vinay Jain Sep 19 '16 at 12:46
  • Its too late - you have already read it and it has already been discarded by the time you hit the `@{ TempData.Keep("Name"); }` line of code –  Sep 19 '16 at 12:47
  • where it has been discarded? first I have display the value at screen

    @TempData["Name"]

    and then I persist the data through keep method. @{ TempData.Keep("Name"); }
    – Vinay Jain Sep 19 '16 at 12:51
  • Your read it in the `@TempData["Name"]` line (and as soon as its read its marked for deletion unless you use `.Keep()` or `.Peek()` before) –  Sep 19 '16 at 12:53
  • in this case, what should I do, should I keep first then read the data from temp data? – Vinay Jain Sep 19 '16 at 12:55
  • That's one option (but why are you using `TempData` like this anyway?) –  Sep 19 '16 at 13:02
  • it works always i have tested because on refresh (F5) it again going to call Index1 and again store tempdata value :-) – Sandip - Frontend Developer Sep 19 '16 at 13:05

2 Answers2

0

it works always, I have tested. Because on refresh (F5) it again going to call Index1 action method and again store tempdata value and load view with tempdata.

if you traverse between view without refresh then it may lost tempdata value

0

Temp data example(Index):-

      if (RouteId != null)
        {
            HttpContext.Session.SetString("RouteId", RouteId);
        }
        else
        {
            RouteId = HttpContext.Session.GetString("RouteId");

            if (RouteId == null)
            {
                //return to the View and display an error message
                TempData["message"] = "Please select a route.";
                return RedirectToAction("Index", "BusRoute");
            }
        }
        var routeStopViewModel = from routeStop in _context.RouteStop.Include(rs => rs.BusRouteCode)
                                 select new RouteStopModel
                                 {
                                     BusRouteCode = routeStop.BusRouteCode
                                 };


        var routeStopsContext = _context.RouteStop
            .Include(rs => rs.BusStopNumberNavigation)
            .Where(predicate: rs =>rs.BusRouteCode==RouteId)
            .OrderBy(keySelector: rs => rs.OffsetMinutes).ToListAsync();

Edit in the Controller:-

         public async Task<IActionResult> Edit(int id, 
         [Bind("RouteStopId,BusRouteCode,BusStopNumber,OffsetMinutes")] 
         RouteStop 
         routeStop)
       {
        if (id != routeStop.RouteStopId)
        {
            return NotFound();
        }

        if (ModelState.IsValid)
        {
            try
            {
                _context.Update(routeStop);
                await _context.SaveChangesAsync();
            }
            catch (DbUpdateConcurrencyException)
            {
                if (!RouteStopExists(routeStop.RouteStopId))
                {
                    return NotFound();
                }
                else
                {
                    throw;
                }
            }
            return RedirectToAction(nameof(Index));
        }
        ViewData["BusRouteCode"] = new SelectList(_context.BusRoute, 
        "BusRouteCode", "BusRouteCode", routeStop.BusRouteCode);
        ViewData["BusStopNumber"] = new SelectList(_context.BusStop, 
        "BusStopNumber", "BusStopNumber", routeStop.BusStopNumber);
        return View(routeStop);

       CLubs (Edit):-
         if (id != club.ClubId)
        {
            return NotFound();
        }

        if (ModelState.IsValid)
        {
            try
            {
                _context.Update(club);
                await _context.SaveChangesAsync();
            }
            catch (DbUpdateConcurrencyException)
            {
                if (!ClubExists(club.ClubId))
                {
                    return NotFound();
                }
                else
                {
                    throw;
                }
            }
            return RedirectToAction(nameof(Index));
        }
        ViewData["StyleCode"] = new SelectList(_context.Style, 
         "StyleCode", 
         "StyleCode", club.StyleCode);
        return View(club); 
        }