Where is viewbag defined
Net Webforms and Asp. Description: In Asp. What is ViewData? ViewData is an in-built dictionary object that is derived from ViewDataDictionary class that can be accessed and set with string type key values. It is a used to pass data from Controller to View. The data is only alive for one request and cannot persist between requests i.
The problems with ViewData are that the type casting is required for complex data types at the location where the data is being extracted and also we need to check for null values to avoid errors. Key points to remember about ViewData. What is ViewBag? The dynamic object is accessed through the Controller.
ViewBag property. Let us see how the ViewBag works through a small example. Write the following code in Index. Message When using ViewBag we can send multiple objects to the view.
Passing Data with ViewData. Similarities between ViewData and ViewBag Helps to maintain data when you move from controller to view. Used to pass data from controller to view.
Short life which means value becomes null after redirection. ViewData requires typecasting for complex data type and check for null values to avoid error.
Asynchronous actions in MVC 5 What does it mean? Unit testing in. You can assign any number of properties and values to ViewBag. If you assign the same property name multiple times to ViewBag, then it will only consider last value assigned to the property. The following example demonstrates how to transfer data from controller to view using ViewBag.
In the above example, we want to display the total number of students in a view. Count value. In other words, as soon as the view is rendered in the browser the ViewData object is emptied.
The following code shows an action method that sets a ViewData key named message with a developer defined message. The Index action method creates an instance of Customer class.
The Customer object thus created is supposed to act as the model for the Index view. Then the code stores message key in the ViewData dictionary with its value set to This is a test message. Finally, the action method returns Index view by calling View method and passing Customer model to it. The following code from the Index view shows how ViewData dictionary can be accessed inside a view. Notice the code marked in bold letters. This data model can be accessed using Model property as shown by Model.
CustomerID line. Next, the code outputs the value of message ViewData key using the dictionary syntax. The last line marked in bold letters may surprise you. It uses Model property of ViewData object to access the model associated with the view.
So, ViewData provides a pointer to the model although you will use it rarely in this fashion because the model is directly available to a view through Model property. In the preceding example, you stored a string value in ViewData. You could have stored any other type of data such as integer, Boolean or object. The following code stores CustomerInfo object into ViewData:.
The above code creates Customer model as before. Additionally, it creates an instance of CustomerInfo class. It then sets a ViewData key named extrainfo to this CustomerInfo object. Whatever you store inside ViewData gets stored as a generic object and you must typecast it while retrieving it inside a view. The following code shows how:. As you can see the above code residing in the Index view retrieves extrainfo from ViewData and typecasts it to CustomerInfo class.
You can then use properties of CustomerInfo class to display their values in the view. Remember that you also need to check for null values in your view in case you expect ViewData entries to be null.
ViewBag is a wrapper over ViewData and allows you to store and retrieve values using object-property syntax rather than key-value syntax used by dictionary objects. It does so using the dynamic data type feature of. In addition to providing object-properties syntax ViewBag also saves you from the job of typecasting as you did with ViewData object. Let's see how ViewBag can be used:.
As you can see, this time the code uses ViewBag object to store CustomerInfo object. It does so by declaring a developer defined property ExtraInfo and setting it to info object.
If you observe ViewData object in Quick Watch window you will see this:.
0コメント