コード例 #1
0
ファイル: ArticleController.cs プロジェクト: uIugbek/Portal
        public IActionResult Put([FromBody] ArticleViewModel model)
        {
            if (ModelState.IsValid && Validate(model))
            {
                Article entity = Activator.CreateInstance <Article>();
                entity = Service.AsObjectQuery().AsNoTracking().FirstOrDefault(f => EqualityComparer <int> .Default.Equals(f.Id, model.Id));

                try
                {
                    model.GetKeys(entity);
                    Mapper.Map <ArticleViewModel, Article>(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));
        }