控制器

时间:2019-10-17 11:48:25

标签: c# .net razor model-view-controller

将我的应用程序运行到/ AboutStore时出现此常见错误。 这是我的代码,我不明白为什么会这样。我将在这里留下我的代码,这里是一个具有IsStoreOpen方法的控制器,我将在视图中显示数据。 任何帮助都很好,谢谢。

public class AboutStoreDetailModel
{
    public int Id { get; set; }
    public string Address { get; set; }
    public string Name { get; set; }
    public string OpenDate { get; set; }
    public string Telephone { get; set; }
    public bool IsOpen { get; set; }
    public string Description { get; set; }
    public int NumberOfAssets { get; set; }
    public decimal TotalAssetValue { get; set; }
    public string ImageUrl { get; set; }
    public IEnumerable<string> HoursOpen { get; set; }
}

   public IActionResult Index()
    {
        var stores = _store.GetAll()
            .Select(s => new AboutStoreDetailModel
            {
                Id = s.Id,
                Name = s.Name,
                IsOpen = _store.IsStoreOpen(s.Id),
                NumberOfAssets = _store.GetAssets(s.Id).Count(),
                Description = s.Description,
                Address = s.Address,
                Telephone = s.Telephone,
                ImageUrl = s.ImageUrl
            });

        var model = new AboutStoreIndexModel()
        {
            Stores = stores
        };

        return View(model);
    }

    public IActionResult Detail(int id)
    {
        var store = _store.Get(id);

        var model = new AboutStoreDetailModel
        {
            Id = store.Id,
            Name = store.Name,
            Address = store.Address,
            Telephone = store.Telephone,
            OpenDate = store.OpenDate.ToString("yyyy-MM-dd"),
            NumberOfAssets = _store.GetAssets(id).Count(),
            TotalAssetValue = _store.GetAssets(id).Sum(a => a.Price),
            ImageUrl = store.ImageUrl,
            HoursOpen = _store.GetStoreHours(id)
        };

        return View(model);
    }

 @foreach (var store in Model.Stores)
            {
            <tr>
                <td class="strongTd"><a asp-controller="AboutStore" asp-action="Detail" asp-route-id="@store.Id">@store.Name</a></td>


>>NullReferenceException: Object reference not set to an instance of an object.
StoreServices.AboutStoreService.IsStoreOpen(int branchId) in AboutStoreService.cs, line 70

>StoreServices.AboutStoreService.IsStoreOpen(int branchId) in AboutStoreService.cs
+
            return currentTimeHour < daysHours.CloseTime && currentTimeHour > daysHours.OpenTime;            
FITMECORE.Controllers.AboutStoreController.<Index>b__2_0(StoreBranch s) in AboutStoreController.cs
+
                .Select(s => new AboutStoreDetailModel
System.Linq.Enumerable+SelectEnumerableIterator<TSource, TResult>.MoveNext()
AspNetCore.Views_AboutStore_Index.<ExecuteAsync>b__11_1() in Index.cshtml
+
                @foreach (var store in Model.Stores)
Microsoft.AspNetCore.Razor.Runtime.TagHelpers.TagHelperExecutionContext.SetOutputContentAsync()
AspNetCore.Views_AboutStore_Index.ExecuteAsync() in Index.cshtml
+
    ViewBag.Title = "About Store Index";

编辑:

此方法返回时发生错误:

public bool IsStoreOpen(int branchId)
        {
            var currentTimeHour = DateTime.Now.Hour;

            var currentDayOfWeek = (int)DateTime.Now.DayOfWeek + 1;

            var hours = _context.StoreHours
                .Where(h => h.Branch.Id == branchId);

            var daysHours = hours
                .FirstOrDefault(h => h.DayOfWeek == currentDayOfWeek);

            return (currentTimeHour < daysHours.CloseTime) && (currentTimeHour > daysHours.OpenTime);

            // Test
            //return 1 < 5 && 45 > 4;            
        }

当我运行注释中的模拟返回值时,我可以正常工作。 我已经调试了代码,并且return语句中的所有变量都有值,没有变量具有空值,所以我不明白为什么它会给我这个错误

0 个答案:

没有答案