简体   繁体   中英

Call method from repository in controller (ASP.NET MVC)

I have a repository, where I have this method for getting some data. Here is the code:

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;
    }
}

I need to call this method in controller, in this method:

public JsonResult GetStops()
{
}

How I can do this?

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM