We first create the SaveController: Create and Update actions will be added to that controller.
Add a new class in the folder controllers\contact. Name the controller SaveController. Make sure that the SaveController inherits from ControllerBase provided by the framework Koossery.MVCwin.
We are going to add 2 actions. The Init action is shown in the listing 1 below:

public IActionResult Init()
{
    //Retrieving the view data
    SaveViewData saveView = this.GetSessionData(typeof(SaveViewData).Name) as SaveViewData;

    //In creation mode we reset the viewData
    if (saveView.IsCreation) saveView.Reset();

    //Render the SaveView
    return View(typeof(SaveView).Name);
}

The next action is the Save action. The save action will add or modify a specific contact according to the state of the ViewData. The listing 2 illustrates the save action

public IActionResult Save()
{
    SaveViewData saveView;
    try
    {
        //Retrieve the view data
        saveView = this.GetSessionData(typeof(SaveViewData).Name) as SaveViewData;

        //reset le context data
        this.ClearErrorsAndMessage();

        //Save the contact
        DoSave(saveView);

        //Invoking the list action
        return RedirectToAction(typeof(ListController).Name, ApplicationData.InitAction);
    }
    catch (Exception ex)
    {
        arg = new Object[3];
        arg[0] = FrontEndConstants.TECHNICAL_DETAIL;
        arg[1] = ex.Message;

        strMsg = String.Format(XmlUtility.getKeyValueFromXmlConfigFile(errorMessageXmlFileName, "SAVECONTACT", "SAVECONTACT-001"), arg);

        log.Error(strMsg);
        String strReturnMessage = strMsg.Substring(0, strMsg.IndexOf(FrontEndConstants.TECHNICAL_DETAIL));
        this.AddMessage(strReturnMessage, MessageType.ERROR_MESSAGE);

        //display the saveView
        return this.View(typeof(SaveView).Name);
    }            
}

If an exception is caught, a special routine is designed to handle the error. We first initialize the argument that will be used to build the error message. The argument is a table of object that contains 2 elements: the constant TECHNICAL_DETAIL and the exception message. The next step is to build the message using the getKeyValueFromXmlConfigFile method of the XmlUtility class provided by the koossery.MVCwin MVC Win framework. The method takes as argument the xml file path where messages are read, the xml element and the xml attribute and return the corresponding message. From that message we extract the friendly user message (the understandable message that will be displayed to the user) and register it as an error message. Finally we displayed the same SaveView to the user to help him correct errors.

Now we need to create the xml file that will contain error message. Right click on the config\messages folder and add an XML File. Name the file FrontEndMessage.xml. Edit the file so that it looks like the code below:

<?xml version="1.0" encoding="utf-8" ?>
<messages>
  <SAVECONTACT SAVECONTACT-001="[SAVECONTACT-001] An error occured while saving the contact. {0} : {1}"/>
</messages>


Now we should create the Delete action in the ListController and write the code for deleting a specific contact:
public IActionResult Delete()
{
    //Getting the listViewData
    ListViewData listData = this.GetSessionData(typeof(ListViewData).Name) as ListViewData;

    //Deleting the contact
    repository.DeleteContact(listData.Contact);

    //Display the ListView
    return RedirectToAction(typeof(ListController).Name, ApplicationData.InitAction);
}

Last edited Jun 22, 2009 at 11:42 AM by koossery, version 2

Comments

No comments yet.