Transferring data from an action method to a template view
The data from the controller's method-action to the view can be passed in two basic ways:
• Using an untyped ViewBag (or ViewData) dictionary; an auxiliary method of data transfer;
• using the object (model) passed through the parameter of the ViewQ method - the main way of data transfer.
Transferring data using ViewData and ViewBag objects
ViewData and ViewBag objects are available in controller methods and in views and allow you to transfer data from the controller to the view. ViewData is a regular collection that allows you to save different values (simple or complex) for a given key. For example:
// using ViewData
ViewDatafPageTitle ] = Programming ASP.NET MVC & quot ;;
The ViewBag object allows you to dynamically assign arbitrary properties to it and assign different values to it. For example, the same data that was passed in the previous example using ViewData can be passed using the new ViewBag object property:
// Using ViewBag
ViewBag.PageTitle = Programming ASP.NET MVC & quot ;;
An example of data transfer using ViewBag can be seen in the description of the Index-method (Figure 4.34) and its corresponding representation (Figure 4.40). In this view, a table is created (the & lt; table & gt; tag) in which the book data will be displayed. The @foreach operator specifies a loop through the collection elements of the ViewBag.Books property, which was previously created in the Index (int id) method of the controller. At each iteration of the loop, the properties of the next element (b.Name, b.Author and b.Price) are displayed in the cells of the table. The last column of the table shows the hyperlink & lt; a href = /Home/Buyfirstname.lastname@example.org" & gt; Buy & lt;/a & gt; indicating the address where you want to transfer to purchase the selected book.
Fig. 4.40. A non-strongly typed representation of the duration of an Index ()
As a result of this presentation, the web page shown in Fig. 4.41.
Fig. 4.41. The result of the execution of the representation shown in Fig. 4.40
Another example of a non-strongly typed representation for the [HttpGet] Buy (int id) method of the HomeController (Figure 4.35) using the ViewData collection is shown in Fig. 4.42.
Fig. 4.42. A non-strongly typed representation for the action [HttpGet] Buy (int id)
Data transfer using the model (strictly-typed views)
Another way to transfer data is to use the Model property of the base presentation class. In this case, the template entry is more readable and the data being transmitted is of a particular type.
In the Razor system, the @model directive is used to declare the type of the transmitted object in the view template. For example:
To access an object passed from the controller action method, you must use the Model property, which is defined in the WebViewPage base class (ViewPage for ASPX).
For example, change the Index method of the HomeController controller (shown in Figure 4.34) so that it uses a strongly typed view (Figure 4.43).
Fig. 4.43. Description of the controller's Index action Note that sends data to the view using the model
Let's create for him a strongly typed representation, shown in Fig. 4.44.