コード例 #1
0
        public IActionResult Put([FromBody] TrendViewModel 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 trend = model.Adapt <Trend>();

            //override properties that should be set in the client side
            //the fields shown below are required.
            //trend.StoreId = model.StoreId;
            trend.Text  = model.Text;
            trend.Notes = model.Notes;
            trend.Views = model.Views;

            // override those properties
            // that should be set from the server-side only
            trend.CreatedDate      = DateTime.Now;
            trend.LastModifiedDate = trend.CreatedDate;

            // add the new trend
            DbContext.Trends.Add(trend);
            // persist the changes into the Database.
            DbContext.SaveChanges();

            // return the newly-created Result to the client.
            return(new JsonResult(trend.Adapt <TrendViewModel>(), JsonSettings));
        }