Guruji Point - Code You Want To Write

  • Home
  • Asp.Net
  • WCF
  • SQL
  • Type Script
  • AngularJS
  • Tech News
  • Blog
  • Earn Online

Thursday, 7 September 2017

View Data ViewBag And TempData Difference In MVC

 Difference Between ViewData, guruji point, Gurujipoint, ViewBag and TempData     No comments   

Introduction
In this article we will learn what is viewdata , viewbag and TempData. How to use ViewData in mvc. How to use ViewBag in MVC. How to use TempData in MVC.  ViewData and ViewBag and TempData example in mvc. Difference between Tempdata,Viewdata and ViewBag in mvc.


ViewData
1.  ViewData is used to pass data from controller to view
2.  ViewData is derived from the ViewDataDictionary class and is basically a Dictionary object i.e.           Keys and Values where Keys are String while Values will be objects.
3.  Data is stored as Object in ViewData.
4.  Data typcasting is required while retrieving the data from ViewData because of its Object type of         data and also null vvalue check befor type casting otherwise it will break the application.
If redirection occurs, then its value becomes null

public ActionResult Index()
{
    ViewData["UName"]= "GurujiPoint.com";
    return View();
}
Now retrieve ViewData value in View
<html>
<head>
    <meta name="viewport" content="width=device-width"/>
    <title>Index</title>
</head>
<body>
    <div>
        @ViewData["UName"]
    </div>
</body>
</html>

ViewBag
1.  ViewBag is just a Wrapper of ViewData.Using pass data from controller to respective View.
2.  ViewBag is a dynamic property and it makes use of the C# 4.0 dynamic features.
3.  No typeCasting required while retrieving the data from Controller.
4.  Available only for the current request.
public ActionResult Index()
{
    ViewBag.UName = "GurujiPoint.com";
    return View();
}

Retrieve the Value in View
<html>
<head>
    <meta name="viewport" content="width=device-width"/>
    <title>Index</title>
</head>
<body>
    <div>
        @ViewBag.UName
    </div>
</body>
</html>

TempData
1.  TempData is Key-Value Dictionary collection
2.  TempData is a dictionary object and it is property of controllerBase class.
3.  TempData is used to pass data from the current request to the next requestIt keeps the information for the time of an HTTP Request. This means only from one page to another. It helps to maintain the data when we move from one controller to another controller or from one action to another action
4.  It requires typecasting for complex data type and checks for null values to avoid error. Generally, it is used to store only one time messages like the error messages and validation messages

public class MySecondController : Controller
{
   public ActionResult Index()
  {
    TempData["UName"] = "GurujiPoint.com";
    return new RedirectResult(@“~\MySecond\“);
   }
}
This is my second controller to get the TempData value on Second controllers view.
public class MySecondController : Controller
{
   public ActionResult Index()
  {
    return View();
   }
}
Now here you can use the TempData Value on Second Controller named MySecond.
<html>
<head>
    <title>Index</title>
</head>
<body>
    <div>
        @TempData["UName"];
    </div>
</body>
</html>


Also Read -
In previous articles we have learnt  Transaction Commit and Rollback in sql server with example.What is Lock and how to achieve lock on sql table. Authorization in Asp.Net. How high quality content affects your Website. What is Blocking and Deadlock In SQL. Top 30 Asp.net interview question. What is the difference between null and undefined.
Check for null vs undefined in javascript.




  • Share This:  
  •  Facebook
  •  Twitter
  •  Google+
  •  Stumble
  •  Digg
Email ThisBlogThis!Share to TwitterShare to Facebook
Newer Post Older Post Home

0 comments:

Post a Comment

Facebook Updates

Guruji Point

Categories

comma seperated string in sql Comman table Expression in SQL Dynamic Grid Row IQueryable in c# Learn AngularJS row_number() and dense_rank() salary table structure. Scope_Identity() Use of indexes while loop in sql why do we need cursor why tuple used

About Us

This blog is about the Programming ,Tech News and some intresting facts related Contents. Here you can find fundamental and expert level topic of programming languages very practically related to C# , Asp.Net ,Sql-Server, JavaScript, Jquery, WebServices And also Web-Api, WCF ,WPF, Angular Js.

Contact Us

Email Us - gurujipoints@gmail.com
Contact- 8077657477

Reach Us

Copyright © Guruji Point - Code You Want To Write