从数据库显示程序列表时出错

时间:2016-12-27 09:12:55

标签: c# sql-server entity-framework linq model-view-controller

这里我试图在list.cshtml中显示数据库中的程序列表。我无法获得节目清单。

如果代码中有任何错误,请告诉我。我在cacheUtilities中遇到ArgumentNullException问题。有人帮我解决了代码问题,从数据库中获取程序列表。

控制器代码

    [HttpGet]
    [Authorize(Roles = "Affiliate")]
    public ActionResult GetTrainingPrograms(int? affiliateId, string searchStr, bool? active, string sort, int? start, int? limit)
    {
        int affId = UserData.AffiliateID;
        if (affiliateId != null && affiliateId.Value > 0)
        {
            affId = affiliateId.Value;
        }

        string searchStrLower = string.Empty;
        string queryStr = String.Concat(affId);
        // The start point in the list of Training Programs
        if (start != null)
        {
            queryStr = String.Concat(queryStr, "-", start.Value);
        }
        // To find Active or Inactive at the Current Time
        if (active != null)
        {
            queryStr = String.Concat(queryStr, "-", active);
        }
        // Sort the List of Data
        if (sort == null)
        {
            sort = "trainingProgramName";

        }
        queryStr = String.Concat(queryStr, "-", sort);
        // limit on the page to Display
        if (limit != null)
        {
            queryStr = String.Concat(queryStr, "-", limit.Value);
        }
        //The keywords used to find a Training Program
        if (!string.IsNullOrEmpty(searchStr))
        {
            searchStrLower = searchStr.ToLower();
            queryStr = String.Concat(queryStr, "-", searchStrLower);
        }

        logger.Debug("trainingProgramListKey: " + queryStr);

        TrainingProgramList reloadData = CacheUtilities.Instance.GetTrainingProgramList(affiliateId);
        logger.Debug("reloadData: " + reloadData + ", affId: " + affId + ", queryStr: " + queryStr);
        string trainingProgramCacheKey = CbConstants.TrainingProgramCacheKey + queryStr;

        TrainingProgramList trainingProgramList = null;

        int totalCount = 0;
        // Checks the List is null or not to Display
        if (trainingProgramList != null)
        {
            logger.Debug("Cache miss for TrainingProgram list: " + trainingProgramCacheKey);

            trainingProgramList = new TrainingProgramList();
            trainingProgramList.AffiliateID = affId;
            totalCount = TrainingProgramRepository.Instance.GetTrainingProgramsCount(affId, searchStrLower, active);
            trainingProgramList.Entries = TrainingProgramRepository.Instance.GetTrainingPrograms(affId, searchStrLower, active, sort, start, limit);

            HttpRuntime.Cache.Insert(trainingProgramCacheKey, trainingProgramList, null, Cache.NoAbsoluteExpiration, CbConstants.CacheSlidingExpirationOneDay);
        }


        CbJsonResponse response = new CbJsonResponse();
        response.Data.Add(trainingProgramList);
        response.Status = "success";
        response.Meta.Add("total", Convert.ToString(totalCount));

        return Json(response, "application/json", System.Text.Encoding.UTF8, JsonRequestBehavior.AllowGet);
    }
}

}

存储库代码:

    public List<TrainingProgramListEntry> GetTrainingPrograms(int affiliateId, string searchStr, bool? active, string sort, int? start, int? limit)
    {
        using (var ctx = new CrossfitPortalEntities())
        {
            ctx.Database.Log = s => System.Diagnostics.Debug.WriteLine(s);

            // Don't need proxies when explicitly loading.
            ctx.Configuration.ProxyCreationEnabled = false;

            var query = ctx.TrainingPrograms.Where(m => m.AffiliateID == affiliateId);

            //The keywords used to find a Training Program
            if (!string.IsNullOrEmpty(searchStr))
            {
                query = query.Where(m => m.Name.ToLower().Contains(searchStr));
            }

            if (active != null)
            {

            }
            // The property on which to sort this list.
            if (sort != null)
            {
                switch (sort)
                {
                    case "TrainingProgram Name":
                        query = query.OrderBy(m => m.Name);
                        break;
                }
            }
            //The number of Training Programs to display on this page.
            if (limit != null)
            {
                logger.Debug("TrainingProgram query limit: " + limit.Value);
            }

            var entries = query.Select(m => new TrainingProgramListEntry
            {
                Name = m.Name,
                ColorCode = m.ColorCode,
       // The start point in the list of Training Programs
            }).Skip((start != null) ? start.Value : 0).Take((limit != null) ? limit.Value : 10).ToList();

            return entries;
        }
    }

CacheUtilites代码:

    public TrainingProgramList GetTrainingProgramList(int? affiliateID)
    {

        string trainingProgramCacheKey = CbConstants.TrainingProgramCacheKey + affiliateID;
        TrainingProgramList trainingprogram = (TrainingProgramList)HttpRuntime.Cache[trainingProgramCacheKey];
        if (trainingprogram != null)
        {
            logger.Debug("Cache hit for AffProfile Entity: " + trainingProgramCacheKey);
        }
        else
        {
            logger.Debug("Cache miss for AffProfile Entity: " + trainingProgramCacheKey);
             trainingprogram = TrainingProgramRepository.Instance.GetTrainingPrograms(AffiliateID);


            HttpRuntime.Cache.Insert(trainingProgramCacheKey, trainingprogram, null, Cache.NoAbsoluteExpiration, CbConstants.CacheSlidingExpirationTwoHours);
        }

        return trainingprogram;
    }

0 个答案:

没有答案