Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

what is the scope of Viewbag in mvc3 and how do we maintain state in MVC

  1. what is the scope of Viewbag in mvc3 is it only available on the page we are rendering through my action method.
  2. How do we maintain information across the page in MVC. Suppose i Create new employee and and when I move to next page I want that employee information.
  3. How do we maintain state in MVC.
like image 767
Mathew Avatar asked Dec 13 '12 17:12

Mathew


People also ask

What is the scope of ViewBag?

The scope of ViewBag is permitted to the current request and the value of ViewBag will become null while redirecting. ViewData is a dictionary object to pass the data from Controller to View where data is passed in the form of key-value pair.

How does ViewBag work in MVC?

In general, ViewBag is a way to pass data from the controller to the view. It is a type object and is a dynamic property under the controller base class. Compared to ViewData, it works similarly but is known to be a bit slower and was introduced in ASP.NET MVC 3.0 (ViewData was introduced in MVC 1.0).

What is the scope of TempData in MVC?

TempData in ASP.NET MVC is basically a dictionary object derived from TempDataDictionary . TempData stays for a subsequent HTTP Request as opposed to other options ( ViewBag and ViewData ) those stay only for current request. So, TempdData can be used to maintain data between controller actions as well as redirects.

What is the use of ViewBag and ViewData in MVC?

ViewData and ViewBag are used for the same purpose -- to transfer data from controller to view. ViewData is nothing but a dictionary of objects and it is accessible by string as key. ViewData is a property of controller that exposes an instance of the ViewDataDictionary class. ViewBag is very similar to ViewData.


1 Answers

  1. the view bag is part of the httpcontext. it's primarily set in the controller action and read in the view, but it can be accessed from just about anywhere in the mvc framework within an http request/response.
  2. the web does not have state, like you would in a rich client app. To maintain values from page to page (or more appropriately, request to request) you can use cookies, session, query string, the request body (think post/put requests).
  3. same as #2.
like image 55
Jason Meckley Avatar answered Oct 04 '22 18:10

Jason Meckley