// GET: Vehicles/Create
        public ActionResult Create()
        {
            // Handles HTTP GET, and returns a view with an HTML Form
            // Notice that we're initializing an object to push initial data into the view

            var newItem = new VehicleAdd {
                ModelYear = DateTime.Now.Year
            };

            return(View(newItem));
        }
Пример #2
0
        public VehicleBase AddVehicle(VehicleAdd newItem)
        {
            // Calculate the next value for the identifier
            int newId = (vehicles.Count > 0) ? newId = vehicles.Max(id => id.Id) + 1 : 1;

            // Create a new item; notice the property mapping
            var addedItem = new Vehicle
            {
                Id        = newId,
                Model     = newItem.Model,
                Trim      = newItem.Trim,
                ModelYear = newItem.ModelYear,
                MSRP      = newItem.MSRP
            };

            // Add the new item to the store
            vehicles.Add(addedItem);

            // Return the new item
            return(Mapper.Map <VehicleBase>(addedItem));
        }
        public ActionResult Create(VehicleAdd newItem)
        {
            // Handles HTTP POST, and adds the new item, then redirects to another view
            // Notice that this method is prefixed with an "attribute", [HttpPost]

            VehicleBase addedItem = null;

            // Check that the incoming data is valid
            if (ModelState.IsValid)
            {
                addedItem = m.AddVehicle(newItem);
            }
            else
            {
                // Return the object so the user can edit it correctly
                return(View(newItem));
            }

            // If the incoming data is valid and the new data was added, redirect
            return(RedirectToAction("index"));

            // Another alternative is to redirect to the "details" view, like this...
            //return RedirectToAction("details", new { id = addedItem.Id });
        }