Example #1
0
        public IActionResult Put([FromBody] OrderViewModel model)
        {
            // return a generic HTTP Status 500 (Server Error)
            // if the client payload is invalid.
            if (model == null)
            {
                return(new StatusCodeResult(500));
            }

            // map the ViewModel to the Model
            var order = model.Adapt <Order>();

            // override those properties
            //   that should be set from the server-side only
            //order.ProductId = model.ProductId;
            order.Text  = model.Text;
            order.Value = model.Value;
            order.Note  = model.Note;

            //properties set from server-side
            order.CreatedDate      = DateTime.Now;
            order.LastModifiedDate = order.CreatedDate;

            // add the new answer
            DbContext.Orders.Add(order);
            // persist the changes into the Database.
            DbContext.SaveChanges();

            // return the newly-created Answer to the client.
            return(new JsonResult(order.Adapt <OrderViewModel>(), JsonSettings));
        }