public async Task <IActionResult> AddTemplate(Template template) { if (ModelState.IsValid) { User user = null; string userId = HttpContext.Session.GetString("UserId"); SessionHandler.GetUser(userId, out user); if (!_context.IsTemplateNameUnique(template.Name, user.Id)) { ViewBag.ErrName = "Template name must be unique."; return(View(template)); } template.UserId = user.Id; try { _context.Add(template); await _context.SaveChangesAsync(); return(RedirectToAction(nameof(TemplateList))); } catch { return(Redirect("/Home/Error")); } } return(View(template)); }