コード例 #1
0
        public IActionResult Put([FromBody] NewsViewModel model)
        {
            if (ModelState.IsValid && Validate(model))
            {
                News entity = Activator.CreateInstance <News>();
                entity = Service.AsObjectQuery().AsNoTracking().FirstOrDefault(f => EqualityComparer <int> .Default.Equals(f.Id, model.Id));

                try
                {
                    model.GetKeys(entity);
                    Mapper.Map <NewsViewModel, News>(model, entity);

                    if (Service.TryUpdate(ref entity))
                    {
                        model.AfterUpdateEntity(entity);
                    }

                    return(Ok(entity));
                }
                catch (Exception ex)
                {
                    ModelState.AddModelError("exception", ex.Message);
                    return(BadRequest(ModelState));
                }
            }

            return(BadRequest(ModelState));
        }