
274Microsoft Visual Studio 2010: A Beginner’s Guide
As shown in Figure
Adding a New Customer
Creating a new customer involves presenting a screen for data entry and saving the new data when submitted. When creating a new object, your Controller needs two methods, a get method to initialize an empty Customer and a post method to save the new customer data. Listing
Listing 9-9 Creating a new Customer object
C#:
//
// GET: /Customer/Create
public ActionResult Create()
{
Customer cust = new Customer
{
Birthday = new DateTime(1980, 1, 1)
};
return View(cust);
}
//
// POST: /Customer/Create
[AcceptVerbs(HttpVerbs.Post)]
public ActionResult Create(Customer cust)
{
try
{
if (string.IsNullOrEmpty(cust.Name))
{
ModelState.AddModelError( "Name", "Name is required."); return View();
}