コード例 #1
0
        public ActionResult Create([Bind(Include = "AlbumID,Title,Artist")] Album album)
        {
            if (ModelState.IsValid)
            {
                db.Albums.Add(album);
                db.SaveChanges();
                TempData["Message"] = "Created " + album.Title;
                return(RedirectToAction("Index"));
            }

            return(View(album));
        }
コード例 #2
0
ファイル: BaseController.cs プロジェクト: goddessting/MvcDemo
        public void UiOrderingManager(OrderQuery order, string tableName, string pkName = "Id")
        {
            string query = null;

            if (order.ToPosition > order.FromPosition)
            {
                query = @"Update " + tableName + " set [Order] = [Order]-1 where [Order] between " + order.FromPosition + "+1 and " + order.ToPosition + "";
            }
            if (order.ToPosition < order.FromPosition)
            {
                query = @"Update " + tableName + " set [Order] = [Order]+1 where [Order] between " + order.ToPosition + " and " + order.FromPosition + "-1";
            }

            using (var db = new MvcDemoContext())
            {
                if (query != null)
                {
                    db.Database.ExecuteSqlCommand(query);
                    query = @"UPDATE " + tableName + " SET [Order]=" + order.ToPosition + " WHERE " + pkName + " = " +
                            order.Id + "";
                    db.Database.ExecuteSqlCommand(query);
                }
                db.SaveChanges();
            }
        }
コード例 #3
0
 public ActionResult Create(Employee employee)
 {
     if (ModelState.IsValid)
     {
         if (employee.Position == "Software Engineer" && employee.Salary < 50000)
         {
             throw new Exception("Salary is not matching with Software Engineer position.");
         }
         else if (employee.Position == "Accountant" && employee.Salary < 40000)
         {
             throw new Exception("Salary is not matching with Accountant position.");
         }
         else if (employee.Position == "Senior Sales Executive" && employee.Salary < 40000)
         {
             throw new Exception("Salary is not matching with Senior Sales Executive position.");
         }
         else
         {
             db.Employees.Add(employee);
             db.SaveChanges();
         }
         return(RedirectToAction("Index", "Home"));
     }
     return(View());
 }
コード例 #4
0
        public CarsController(MvcDemoContext ctx)
        {
            _ctx = ctx;

            if (_ctx.Cars.Count() == 0)
            {
                _ctx.Cars.Add(
                    new Car
                {
                    BrandName          = "DMC",
                    ModelName          = "Delorian",
                    YearOfConstruction = 1985
                });
                _ctx.SaveChanges();
            }
        }
        public void OnException(ExceptionContext filterContext)
        {
            var exceptionLogger = new ExceptionLogger()
            {
                ExceptionMessage    = filterContext.Exception.Message,
                ExceptionStackTrack = filterContext.Exception.StackTrace,
                ControllerName      = filterContext.RouteData.Values["controller"].ToString(),
                ActionName          = filterContext.RouteData.Values["action"].ToString(),
                ExceptionLogTime    = DateTime.Now
            };

            db.ExceptionLoggers.Add(exceptionLogger);
            db.SaveChanges();

            filterContext.ExceptionHandled = true;
            filterContext.Result           = new ViewResult
            {
                ViewName = "Error"
            };
        }