How to Use Tempdata with peek and keep functions in asp.net Mvc.

Introduction

In this article we have lerning to, how to use Keep and Peek functions of TempData in ASP.Net MVC Razor.

The Keep function is used to preserve the data of TempData object even after the value is read while the Peek function is used to read the value without clearing it.TempData is used to pass the data from Action to Action or Controller to Controller, and then to View. In case of Action to Action or Controller to Controller, ViewData and ViewBag do not persist the data. It keeps the information for a single HTTP Request. It is derived from TemDataDictionary and along with this, it requires typecasting for complex data types and it requires a null check to void exceptions.

Simple tempdata :

When value is read from the TempData object, the value is cleared and NULL value is assigned.

Controller Code :

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace PeekandKeekMethods.Controllers
{
    public class HomeController : Controller
    {
        [HttpGet]
        public ActionResult Index()
        {
            TempData["CurrentTime"] = DateTime.Now;
            return View();
        }
        [HttpPost]
        public ActionResult ViewMassage()
        {
            var message = TempData["CurrentTime"];
            return View(message);
        }        
    }
}

View :

@{
    ViewBag.Title = "Home Page";
}
<h3>Index</h3>
<h2>Today date : @TempData["CurrentTime"].ToString()</h2>
<form action="/Home/ViewMassage" method="post">
    <input type="submit" class="btn-default" value="Submit"/>
</form>

Tempdata result :

TempData object value was read in View and hence it is NULL.

Tempdata With Keep() :

The Keep function is used to preserve the data of Tempdata object even after the value is read.

controller code:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace PeekandKeekMethods.Controllers
{
    public class HomeController : Controller
    {
        [HttpGet]
        public ActionResult Index()
        {
            TempData["CurrentTime"] = DateTime.Now;
            return View();
        }
        [HttpPost]
        public ActionResult ViewMassage()
        {
            var message = TempData["CurrentTime"];
            return View(message);
        }        
    }
}

View :

@{
    ViewBag.Title = "Home Page";
}
<h3>Index</h3>
@TempData["CurrentTime"]

@{TempData.Keep("CurrentTime");}
<form action="/Home/ViewMassage" method="post">
    <input type="submit" class="btn btn-info" value="Submit"/>
</form>

Tempdata With Keep() result :

Tempdata With Peek() :

The Peek function is used to read the data of  Tempdata object without clearing it.returns an object that contains the element that is associated with the specified key, without marking the key for deletion.

controller code:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace PeekandSeekMethods.Controllers
{
    public class HomeController : Controller
    {
        [HttpGet]
        public ActionResult Index()
        {
            TempData["CurrentTime"] =DateTime.Now;
            return View();
        }
        [HttpPost]
        public ActionResult ViewMassage()
        {
            var message = TempData["CurrentTime"];
            return View(message);
        }        
    }
}

View :

@{
    ViewBag.Title = "Home Page";
}
<h3>Index</h3>
@TempData["CurrentTime"]

@{TempData.Peek("CurrentTime");}
<form action="/Home/ViewMassage" method="post">
    <input type="submit" class="btn btn-info" value="Submit"/>
</form>

TempdataWith Peek() result :

You can use keep() when you always want to retain the value for another request. Use Peek() when retaining the value depends on additional logic.

Submit a Comment

Your email address will not be published. Required fields are marked *

Subscribe

Select Categories