Table of contents
- 1 Listing
- 2 Creating
- 3 Updating
- 4 Deleting
Creating a CRUD page with DataBinder
CRUD stands for Create, Retrieve, Update and Delete. Most applications have some kind of CRUD funcionality, and we can assume that every programmer had to deal with CRUD at some point.
As you remember, we have created a Supplier and Product classes. We used scaffolding to create the crud for the Supplier class. Now let's code the CRUD for the Product class manually. You'll see that it is also something really simple.
The first thing you must do is create the ProductController:
namespace GettingStartedSample.Controllers { using System; using Castle.MonoRail.Framework; using GettingStartedSample.Models; [Layout("default"), Rescue("generalerror")] public class ProductController : SmartDispatcherController { } }
Now we are ready.
Listing
Create a List action on the controller:
public void List() { PropertyBag["products"] = Product.FindAll(); }
This code makes all products available to the view. Now create the list.vm:
<h3>Product list</h3> <p> <a href="new.rails">Create new Product</a> </p> <table width="100%" border="1" cellpadding="2" cellspacing="0"> <tr> <th>Id</th> <th>Name</th> <th>Supplier</th> <th> </th> </tr> #foreach($product in $products) <tr> <td align="center">$product.Id</td> <td align="center">$product.Name</td> <td align="center">$product.Supplier.Name</td> <td align="center"> <a href="edit.rails?id=${product.Id}">Edit</a> | <a href="delete.rails?id=${product.Id}">Delete</a> </td> </tr> #end </table>
Test your working accessing /product/list.rails

Creating
To insert a new product we will use two actions:
- New: which will present the form
- Create: which uses the data supplied on the form to create the Product on the database
public void New() { PropertyBag["suppliers"] = Supplier.FindAll(); } public void Create([DataBind("product")] Product prod) { try { prod.Create(); RedirectToAction("list"); } catch(Exception ex) { Flash["error"] = ex.Message; Flash["product"] = prod; RedirectToAction("new"); } }
The New action makes all suppliers available to the view. This is required to populate a select element with all suppliers available.
The new.vm view follows:
<h3>New Product</h3> #if($Flash.error) <p style="color: red; font-weight: bold;"> $Flash.error </p> #end <form action="create.rails" method="post"> <p> Name: $FormHelper.TextField("product.name") </p> <p> Price: $FormHelper.TextFieldFormat("product.price", "000.00") </p> <p> Supplier: $FormHelper.Select("product.supplier.id", $suppliers, "%{value='Id', text='Name'}") </p> <hr/> <p> <input type="submit" value="Create" /> </p> </form>
Test your working accessing /product/new.rails

Updating
Updating is not very different from creating. First create two actions: Edit and Update.
public void Edit(int id) { PropertyBag["product"] = Product.FindById(id); PropertyBag["suppliers"] = Supplier.FindAll(); } public void Update([DataBind("product")] Product prod) { try { prod.Update(); RedirectToAction("list"); } catch(Exception ex) { Flash["error"] = ex.Message; Flash["product"] = prod; RedirectToAction("edit", "id=" + prod.Id); } }
We can also use a different Edit overload that will be used when the Update catch block redirects it back to the Edit:
public void Edit(int id, [FlashBinder] Product product) { PropertyBag["suppliers"] = Supplier.FindAll(); }
The edit.vm view is pratically the same as the new.vm. The FormHelper is clever enough to populate the fields and select the correct item on the select element.
<h3>Edit Product</h3> #if($Flash.error) <p style="color: red; font-weight: bold;"> $Flash.error </p> #end <form action="update.rails" method="post"> $FormHelper.HiddenField("product.id") <p> Name: $FormHelper.TextField("product.name") </p> <p> Price: $FormHelper.TextFieldFormat("product.price", "000.00") </p> <p> Supplier: $FormHelper.Select("product.supplier.id", $suppliers, "%{value='Id', text='Name'}") </p> <hr/> <p> <input type="submit" value="Update" /> </p> </form>
Deleting
Deleting is the easiest one. Add the Delete action:
public void Delete(int id) { Product product = Product.FindById(id); product.Delete(); RedirectToAction("list"); }
So, do you miss WebForms at all?
Proceed with Getting more.