public ActionResult CatalogAdd(CatalogAdd model) { if (ModelState.IsValid) { var catalog = new Catalog(); catalog.AddTime = DateTime.Now; catalog.Name = model.Name; catalog.Status = true; int res = 0; using (LegendaryBlog.App_Code.BlogDbContext dbContext = new App_Code.BlogDbContext()) { dbContext.Catalogs.Add(catalog); res = dbContext.SaveChanges();//才是真正保存到数据库 } if (res > 0) { return(Content("添加成功")); } else { return(Content("添加失败")); } } else { return(Content("验证失败")); } }
public ActionResult Add(CatalogAdd info) { if (ModelState.IsValid) { // Mapper.Initialize(x => x.CreateMap<CatalogAdd, Catalog>()); var model = Mapper.Map <Catalog>(info); model.AddTime = DateTime.Now; model.EditTime = DateTime.Now; model.BlogID = LoginBlog.ID; model.Status = true; blogsdbContext.catalog.Add(model); int res = blogsdbContext.SaveChanges(); if (res > 0) { return(RedirectToAction("Index")); } } return(View(info)); }