public async Task <ActionResult> Create(IFormCollection collection) { var entity = new Department(); await TryUpdateModelAsync(entity); if (ModelState.IsValid) { try { var result = await _db.InsertIfNotExistsAsync(entity, d => d.Name == entity.Name); if (result > 0) { return(RedirectToAction(nameof(Index))); } else { ModelState.AddModelError(string.Empty, "创建失败"); } } catch (Exception ex) { ModelState.AddModelError(string.Empty, ex.Message); } } return(View(entity)); }