This project is read-only.

ProMesh.NET Quick Walkthrough - Part 4 - Parameters (client data)


How does our controller retrieve data from the client? By client data we mean URL parameters and data posted by controls on our page.

There are 3 ways to pass parameters to the controller:

1. Method parameters

public class EmployeeDetail : PageController
{
   public void Run(int id, string action)
   {
      Employee employee = new Employee(id);
      
      if (action == "delete")
      {
          employee.Delete();
      }
      else
      {
          ViewData["CurrentEmployee"] = employee;
      }
   }
}


In the example above we defined two parameters "id" and "action". These parameters are retrieved from the URL that was requested.

For example, when the URL is http://www.yoursite.com/emplyeedetail.ashx?id=5&action=delete , the "id" and "action" parameters will be passed to the Run() method. If a parameter is not specified, null is passed (when the type of the parameter is not nullable, the default value for the specific data type will passed)

2. Instance fields or properties

Instance fields can also be mapped to parameters by decorating them with the [Get], [Post] or [GetOrPost] attribute:

public class EmployeeDetail : PageController
{
   [Get("id")]
   private int _id;

   public void Run(string action)
   {
      Employee employee = new Employee(_id);
      
      if (action == "delete")
      {
          employee.Delete();
      }
      else
      {
          ViewData["CurrentEmployee"] = employee;
      }
   }
}


In the above example, the _id field is mapped to the id parameter

3. Explicit retrieval of parameters and form data

It is also possible to retrieve parameters and form data by using the GetData and PostData objects:

public class EmployeeDetail : PageController
{
   public void Run()
   {
      int id = GetData.Get<int>("id"); // uses the Get() method (generic version)
      string action = GetData["action"];  // uses the indexer

      Employee employee = new Employee(id);
      
      if (action == "delete")
      {
          employee.Delete();
      }
      else
      {
          ViewData["CurrentEmployee"] = employee;
      }
   }
}


Next >> Forms

Last edited Jul 30, 2007 at 4:29 PM by activa, version 4

Comments

No comments yet.