从控制器中的存储库调用方法(ASP.NET MVC)

时间:2017-10-31 11:17:15

标签: c# asp.net asp.net-mvc

我有一个存储库,我有这个方法来获取一些数据。这是代码:

public List<HeatmapViewModel> GetStops()
{
    using (var ctx = new GoogleMapTutorialEntities())
    {
        List<HeatmapViewModel> items = new List<HeatmapViewModel>();

        #region firstitem_calculation
        var firstitem = ctx.Loggings.Where(x => x.Datatype == 2).AsEnumerable().Select(
            x => new Logging
            {
                Longitude2 = x.Longitude2,
                Latitude2 = x.Latitude2,
                CurDateTime = x.CurDateTime
            }).FirstOrDefault();

        var midnight = new DateTime(firstitem.CurDateTime.Year, firstitem.CurDateTime.Month, firstitem.CurDateTime.Day, 00, 00, 00);
        TimeSpan difference = (firstitem.CurDateTime - midnight);
        var difference_after_midnight = (int) difference.TotalMinutes;

        items.Add( new HeatmapViewModel
        {
            FirstStartDifference = difference_after_midnight
        });
        #endregion

        return items;
    }
}

我需要在控制器中调用此方法,在此方法中:

public JsonResult GetStops()
{
}

我怎么能这样做?

2 个答案:

答案 0 :(得分:3)

YourRepositoryName repo = new YourRepositoryName();
var _data = repo.GetStops();

答案 1 :(得分:1)

public JsonResult GetStops()
{
    var repo = new TheRepository();
    var listOfHeatMapVm = repo.GetStops();

    //Convert the list of HeatMapVm to Json result here.
    return Json(listOfHeatMapVm);
}