Example #1
0
        public ActionResult Create(CustomerAdd newItem)
        {
            // Attention 20 - Notice the pattern for handling incoming data...
            // First, ensure that the incoming data is valid
            // Next, attempt to process the incoming data (add customer etc.)
            // Finally, if successful, redirect to another view
            // This is known as the PRG pattern - Post, Redirect, Get

            // Validate the input
            if (!ModelState.IsValid)
            {
                // Uh oh, problem with the data, show the form again, with the data
                return(View(newItem));
            }

            // Process the input
            var addedItem = m.CustomerAdd(newItem);

            if (addedItem == null)
            {
                // Uh oh, some problem adding, show the empty form again
                return(View(newItem));
            }
            else
            {
                return(RedirectToAction("details", new { id = addedItem.CustomerId }));
            }
        }
Example #2
0
        // Attention 13 - Add new customer
        public CustomerBase CustomerAdd(CustomerAdd newItem)
        {
            // Attempt to add the new item
            // Notice how we map the incoming data to the design model object
            var addedItem = ds.Customers.Add(mapper.Map <Customer>(newItem));

            ds.SaveChanges();

            // If successful, return the added item, mapped to a view model object
            return((addedItem == null) ? null : mapper.Map <CustomerBase>(addedItem));
        }