The purpose of this section is to provide you with a brief introduction to koossery.MVCwin views and view data. By the end of this section, you should understand how to create new views and pass data from a controller to a view.

Understanding Views

koossery.MVCwin provides a BaseView object which extends the .NET Form Class an implement the Koossery.MVCwin.Views.itf.IView interface. All koossery.MVCwin incoming requests are mapped to controller actions. A controller action might return a view. However, a controller action might perform some other type of action such as redirecting you to another controller action.

Listing 1 contains a simple controller named the CustomController. The CustomController exposes two controller actions named Init() and Login().

using System;
using System.Collections.Generic;
using System.Text;
using Koossery.MVCwin.Actions.itf;

namespace Koossery.MVCwin.Tuto.App_Code.controllers.contact
{
    public class CustomController : Koossery.MVCwin.Controller.impl.ControllerBase
    {
        public IActionResult Init()
        {
            return View(typeof(CustomView).Name);
        }

        public IActionResult Login()
        {
            return RedirectToAction(typeof(CustomController).Name, "Init");
        }
    }
}

You can invoke the first action, the Init() action, using the following code:

Invoke(typeof(CustomController).Name, “Init”);

You can invoke the second action, the Login() action, using the following code :

Invoke(typeof(CustomController).Name, “Login”);

The Init() action returns a view. Most actions that you create will return views. However, an action can return other types of action results. For example, the Login() action returns a RedirectToActionResult that redirects incoming request to the Init() action.

The Init() action contains the following single line of code:

return View(typeof(CustomView).Name);

This line of code returns a view named CustomView that will be located by the ControllerManager using the ControllerManager’s Views dictionary. typeof(CustomView).Name is used as the key.

The ControllerManager retrieves the CustomView form and displays it to the end user.

Adding Content to a View

As long as all Views inherit from the Koossery.MVCwin.Views.impl.BaseView, they are considered as classic .NET 2.0 Form. The content is designed using the VS Controls toolbox. You can take advantage of DataBinding or a third party graphical controls.

Using View Data to Pass Data to a View

Inside a controller you user the base.GetSessionData() method to gain access to a particular ViewData. The GetSessionData method takes a key as parameter and return the specified ViewData. The ControllerBase contains a dictionary (SessionData class) of all application’s ViewData.. You use ViewData to store data in order to display them in the view. For example, the controller in Listing 2 adds a message to welcomeViewData data.

public IActionResult Init()
{
      //Getting de view data
      WelcomeViewData welcomeViewData = this.GetSessionData(typeof(WelcomeViewData).Name) as WelcomeViewData;
      welcomeViewData.GreetingMessage = "Welcome to the Koossery.MVCwin Tutorial";
            
      return this.View(typeof(WelcomeView).Name);
 }


When the view is returned by the Init() method, the view data is accessible to the view using the BaseView GetSessionData method.

Last edited Jun 19, 2009 at 5:55 PM by mohamadou, version 5

Comments

No comments yet.