Пример #1
0
        public ActionResult Edytuj(int id)
        {
            var Model = LataObrotoweRepository.RokObrotowy(id);

            if (Model == null)
            {
                return(View("NotFound"));
            }

            return(View(Model));
        }
        public override void OnActionExecuting(ActionExecutingContext filterContext)
        {
            try
            {
                int?CompanyId = null;
                int?YearId    = null;
                int ParsedCompanyId;
                int ParsedYearId;

                // do not make this repositories as a class properties, they has to be instantiated in every request (or you wont see new results)
                FirmyRepository        FirmyRepository        = new FirmyRepository();
                LataObrotoweRepository LataObrotoweRepository = new LataObrotoweRepository();

                // jesli w AppSettings ustawimy idFirmy to zapisujemy je w cache'u
                if (Int32.TryParse(System.Web.Configuration.WebConfigurationManager.AppSettings["idFirmy"], out ParsedCompanyId))
                {
                    CompanyId = ParsedCompanyId;
                    //System.Web.HttpContext.Current.Cache.Insert("CompanyId", ParsedCompanyId);
                }

                // jesli w AppSettings ustawimy idRoku to zapisujemy je w cache'u
                if (Int32.TryParse(System.Web.Configuration.WebConfigurationManager.AppSettings["idRoku"], out ParsedYearId))
                {
                    YearId = ParsedYearId;
                    //System.Web.HttpContext.Current.Cache.Insert("YearId", ParsedYearId);
                }

                //if (!(CompanyId = (int?)System.Web.HttpContext.Current.Cache.Get("CompanyId")).HasValue)
                if (!CompanyId.HasValue && !(CompanyId = FirmyRepository.WybraneIdFirmy(filterContext.HttpContext.User.Identity.Name)).HasValue)
                {
                    string returnUrl = filterContext.HttpContext.Request.Url.ToString();
                    string debug     = filterContext.HttpContext.Request.Params["returnUrl"];
                    filterContext.Controller.TempData["Message"] = String.Format("Nie wybrano firmy.");
                    filterContext.Result = new RedirectToRouteResult(new RouteValueDictionary {
                        { "action", "Wybor" }, { "controller", "Firmy" }, { "returnUrl", returnUrl }
                    });
                }
                else
                {
                    if (!YearId.HasValue && !(YearId = LataObrotoweRepository.WybraneIdRoku(filterContext.HttpContext.User.Identity.Name)).HasValue)
                    {
                        string returnUrl = filterContext.HttpContext.Request.Url.ToString();
                        string debug     = filterContext.HttpContext.Request.Params["returnUrl"];
                        filterContext.Controller.TempData["Message"] = String.Format("Nie wybrano roku.");
                        filterContext.Result = new RedirectToRouteResult(new RouteValueDictionary {
                            { "action", "Wybor" }, { "controller", "LataObrotowe" }, { "idFirmy", CompanyId }, { "returnUrl", returnUrl }
                        });
                    }
                    else
                    {
                        filterContext.Controller.ViewBag.YearId     = YearId;
                        filterContext.Controller.TempData["YearId"] = YearId;

                        //string YearName = (string)System.Web.HttpContext.Current.Cache.Get("YearName");
                        string YearName = LataObrotoweRepository.RokObrotowy(YearId.Value).NazwaRoku;

                        if (!String.IsNullOrEmpty(YearName))
                        {
                            filterContext.Controller.ViewBag.YearName     = YearName;
                            filterContext.Controller.TempData["YearName"] = YearName;
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                Logger.Error(ex);
                filterContext.Result = new RedirectToRouteResult("Firma", null);
            }

            base.OnActionExecuting(filterContext);
        }