S.E
S.E

Reputation: 107

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?

Upvotes: 1

Views: 2700

Answers (2)

Infinity Challenger
Infinity Challenger

Reputation: 1140

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

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

Upvotes: 0

cagri
cagri

Reputation: 64

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

Upvotes: 2

Related Questions