示例#1
0
        // Add new Employee
        public EmployeeBase EmployeeAdd(EmployeeAdd newItem)
        {
            // Attempt to add the new item
            // Notice how we map the incoming data to the design model object
            var addedItem = ds.Employees.Add(mapper.Map <EmployeeAdd, Employee>(newItem));

            ds.SaveChanges();

            // If successful, return the added item, mapped to a view model object
            return((addedItem == null) ? null : mapper.Map <Employee, EmployeeBase>(addedItem));
        }
示例#2
0
        public EmployeeBase EmployeeAdd(EmployeeAdd newItem)
        {
            // Ensure that we can continue
            if (newItem == null)
            {
                return(null);
            }
            else
            {
                // Add the new object
                Employee addedItem = mapper.Map <Employee>(newItem);

                ds.Employees.Add(addedItem);
                ds.SaveChanges();

                // Return the object
                return(mapper.Map <EmployeeBase>(addedItem));
            }
        }
        // POST: api/Employee
        public IHttpActionResult Post([FromBody] EmployeeAdd newItem)
        {
            // Ensure that the URI is clean (and does not have an id parameter)
            if (Request.GetRouteData().Values["id"] != null)
            {
                return(BadRequest("Invalid request URI"));
            }

            // Ensure that a "newItem" is in the entity body
            if (newItem == null)
            {
                return(BadRequest("Must send an entity body with the request"));
            }

            // Ensure that we can use the incoming data
            if (ModelState.IsValid)
            {
                // Attempt to add the new object
                var addedItem = m.EmployeeAdd(newItem);

                // Notice the ApiController convenience methods
                if (addedItem == null)
                {
                    // HTTP 400
                    return(BadRequest("Cannot add the object"));
                }
                else
                {
                    // HTTP 201 with the new object in the entity body
                    // Notice how to create the URI for the Location header

                    var uri = Url.Link("DefaultApi", new { id = addedItem.EmployeeId });
                    return(Created <EmployeeBase>(uri, addedItem));
                }
            }
            else
            {
                // HTTP 400
                return(BadRequest(ModelState));
            }
        }
示例#4
0
        public ActionResult Create(EmployeeAdd newItem)
        {
            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.EmployeeAdd(newItem);

            if (addedItem == null)
            {
                // Uh oh, some problem adding, show the empty form again
                return(View(newItem));
            }
            else
            {
                return(RedirectToAction("details", new { id = addedItem.EmployeeId }));
            }
        }