private async Task <int> CreateArticle(CreateAndEditArticleViewModel vm, AjaxResponse ajaxResponse) { var article = new Article.Article() { Title = vm.Title, Content = Base64.base64Decode(vm.Content), Description = vm.Description, Source = vm.Source, CoverImg = vm.CoverImg, CategoryName = vm.CategoryName, Status = vm.Status ?? 0, CreateTime = DateTime.Now, UpdateTime = DateTime.Now }; var r = await _articleManager.CreateAsync(article); if (r.Succeeded) { return(article.Id); } _logger.LogWarning("添加文章失败"); ajaxResponse.Errors.Add(new ErrorInfo(-1, r.Errors.FirstOrDefault()?.Description ?? "添加文章失败")); return(0); }
public async Task <IActionResult> Edit(int id) { var vm = new CreateAndEditArticleViewModel(); if (id > 0) { var article = await _articleManager.FindByIdAsync(id); if (article != null) { vm.Id = article.Id; vm.Title = article.Title; vm.Content = Base64.base64Encode(article.Content); vm.Description = article.Description; vm.Source = article.Source; vm.CoverImg = article.CoverImg; vm.CategoryName = article.CategoryName; vm.Status = article.Status; } } ViewData["Categorys"] = GetCategorys(); return(View(vm)); }
public async Task <JsonResult> Edit(CreateAndEditArticleViewModel vm) { var ajaxResponse = CreateAjaxResponse(); ajaxResponse.TargetUrl = vm.Id.ToString(); if (ModelState.IsValid) { if (vm.Id > 0) { var article = await _articleManager.FindByIdAsync(vm.Id); if (article != null) { article.Title = vm.Title; article.Description = vm.Description; article.Content = Base64.base64Decode(vm.Content); article.Source = vm.Source; article.CoverImg = vm.CoverImg; article.CategoryName = vm.CategoryName; article.Status = vm.Status ?? 0; var r = await _articleManager.UpdateAsync(article); if (!r.Succeeded) { _logger.LogWarning("修改文章失败"); ajaxResponse.Errors.Add(new ErrorInfo(-1, r.Errors.FirstOrDefault()?.Description ?? "修改文章失败")); } } else { var tempId = await CreateArticle(vm, ajaxResponse); if (tempId > 0) { ajaxResponse.TargetUrl = tempId.ToString(); } } } else { var tempId = await CreateArticle(vm, ajaxResponse); if (tempId > 0) { ajaxResponse.TargetUrl = tempId.ToString(); } } } else { AddErrorsToAjaxResponse(ajaxResponse); } return(Json(ajaxResponse)); }