【问题标题】:ArgumentNullException when indexing IEnumerable object索引 IEnumerable 对象时出现 ArgumentNullException
【发布时间】:2016-12-27 11:17:10
【问题描述】:

我正在创建 ASP.NET MVC 4 应用程序。我有包含 EventsHelper 类的 SocialEvents.BusinessLogic 项目

public class EventsHelper
{
    IRepository repo;

    public EventsHelper(IRepository repository)
    {
        repo = repository;          
    }
    public IEnumerable<Event> GetEvents()
    {
        return repo.Events;
    }   
}

在我的 MVC 项目 SocialEvents 中,我有 HomeController 和 Index 操作

public class HomeController : Controller
{
    private static EventsHelper eventsHelper = new EventsHelper(new Repository());

    public ActionResult Index()
    {                        
        return View(eventsHelper);
    }
}

在索引视图中我正在获取事件

@{ var eventsList = Model.GetEvents();}

后来尝试索引它们

@if (eventsList != null)
{
    foreach (var _event in eventsList)
    {
        <h3>@_event.Title</h3>
    }                            
}

但我在 foreach 中遇到 ArgumentNullException

异常出现在System.Linq.Enumerable.First(IEnumerable'1 source, Func'2 predicate)参数名source

Control values when running step-through debugging

如果 IEnumerable 对象不为空,我不明白为什么会出现此异常以及如何修复它

Repository 类使用 EntityFramework DbContext 类SocialEventsContext

存储库

public class Repository : IRepository
{
    private readonly SocialEventsContext context = new SocialEventsContext();

    public IEnumerable<Event> Events
    {
        get
        {
            return context.Events;
        }
    }
    public void Add<T>(T entity) where T : Entity
    {
        context.Set<T>().Add(entity);
    }
    public void Delete<T>(T entity) where T : Entity
    {
        context.Set<T>().Remove(entity);
    }
    public void SaveChanges()
    {
        context.SaveChanges();
    }
}

社交事件上下文

public class SocialEventsContext : DbContext
{
    public DbSet<Event> Events { get; set; }
}

And in my localDb I actually have data

堆栈跟踪:

[ArgumentNullException: Value cannot be null.
Parameter name: source]
   System.Linq.Enumerable.First(IEnumerable`1 source, Func`2 predicate) +4396142
   ASP._Page_Views_Home_Index_cshtml.Execute() in E:\Documents\Practice\SocialEvents\SocialEvents\Views\Home\Index.cshtml:50
   System.Web.WebPages.WebPageBase.ExecutePageHierarchy() +197
   System.Web.Mvc.WebViewPage.ExecutePageHierarchy() +105
   System.Web.WebPages.StartPage.RunPage() +17
   System.Web.WebPages.StartPage.ExecutePageHierarchy() +64
   System.Web.WebPages.WebPageBase.ExecutePageHierarchy(WebPageContext pageContext, TextWriter writer, WebPageRenderingBase startPage) +78
   System.Web.Mvc.RazorView.RenderView(ViewContext viewContext, TextWriter writer, Object instance) +235
   System.Web.Mvc.BuildManagerCompiledView.Render(ViewContext viewContext, TextWriter writer) +107
   System.Web.Mvc.ViewResultBase.ExecuteResult(ControllerContext context) +291
   System.Web.Mvc.ControllerActionInvoker.InvokeActionResult(ControllerContext controllerContext, ActionResult actionResult) +13
   System.Web.Mvc.ControllerActionInvoker.InvokeActionResultFilterRecursive(IList`1 filters, Int32 filterIndex, ResultExecutingContext preContext, ControllerContext controllerContext, ActionResult actionResult) +56
   System.Web.Mvc.ControllerActionInvoker.InvokeActionResultFilterRecursive(IList`1 filters, Int32 filterIndex, ResultExecutingContext preContext, ControllerContext controllerContext, ActionResult actionResult) +420
   System.Web.Mvc.ControllerActionInvoker.InvokeActionResultWithFilters(ControllerContext controllerContext, IList`1 filters, ActionResult actionResult) +52
   System.Web.Mvc.Async.<>c__DisplayClass2b.<BeginInvokeAction>b__1c() +173
   System.Web.Mvc.Async.<>c__DisplayClass21.<BeginInvokeAction>b__1e(IAsyncResult asyncResult) +100
   System.Web.Mvc.Async.WrappedAsyncResult`1.CallEndDelegate(IAsyncResult asyncResult) +10
   System.Web.Mvc.Async.WrappedAsyncResultBase`1.End() +49
   System.Web.Mvc.Async.AsyncControllerActionInvoker.EndInvokeAction(IAsyncResult asyncResult) +27
   System.Web.Mvc.Controller.<BeginExecuteCore>b__1d(IAsyncResult asyncResult, ExecuteCoreState innerState) +13
   System.Web.Mvc.Async.WrappedAsyncVoid`1.CallEndDelegate(IAsyncResult asyncResult) +29
   System.Web.Mvc.Async.WrappedAsyncResultBase`1.End() +49
   System.Web.Mvc.Controller.EndExecuteCore(IAsyncResult asyncResult) +36
   System.Web.Mvc.Controller.<BeginExecute>b__15(IAsyncResult asyncResult, Controller controller) +12
   System.Web.Mvc.Async.WrappedAsyncVoid`1.CallEndDelegate(IAsyncResult asyncResult) +22
   System.Web.Mvc.Async.WrappedAsyncResultBase`1.End() +49
   System.Web.Mvc.Controller.EndExecute(IAsyncResult asyncResult) +26
   System.Web.Mvc.Controller.System.Web.Mvc.Async.IAsyncController.EndExecute(IAsyncResult asyncResult) +10
   System.Web.Mvc.MvcHandler.<BeginProcessRequest>b__5(IAsyncResult asyncResult, ProcessRequestState innerState) +21
   System.Web.Mvc.Async.WrappedAsyncVoid`1.CallEndDelegate(IAsyncResult asyncResult) +29
   System.Web.Mvc.Async.WrappedAsyncResultBase`1.End() +49
   System.Web.Mvc.MvcHandler.EndProcessRequest(IAsyncResult asyncResult) +28
   System.Web.Mvc.MvcHandler.System.Web.IHttpAsyncHandler.EndProcessRequest(IAsyncResult result) +9
   System.Web.CallHandlerExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute() +9744373
   System.Web.HttpApplication.ExecuteStep(IExecutionStep step, Boolean& completedSynchronously) +155

【问题讨论】:

  • repo.Events 中有什么?你正在使用 IEnumarable,所以这里可能有延迟执行......
  • @OfirW 我更新了我的问题
  • 您实际上可能返回了一个 IQueryable 吗?尝试在 repo 调用结束时添加 ToList()。
  • @ste-fu 喜欢那个return repo.Events.ToList();?
  • 是的。确保查询是针对 thw db 执行的。

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


【解决方案1】:

向您的模型类添加属性,该属性将填充控制器,然后将该模型传递给视图。

型号

public class EventsHelper
    {
        IRepository repo;

        public EventsHelper(IRepository repository)
        {
            repo = repository;          
        }
        public IEnumerable<Event> GetEvents()
        {
            return repo.Events;
        }  

      public List<Event> eventList {get; set;}
    }

控制器

public class HomeController : Controller
{
    private static EventsHelper eventsHelper = new EventsHelper(new Repository());

    public ActionResult Index()
    {             
        eventsHelper.eventList = eventsHelper.GetEvents();
        return View(eventsHelper);
    }
}

查看

@{ var eventsList = Model.eventList }

【讨论】:

  • 对我不起作用。同一行的相同异常
  • 你遇到了什么异常?
  • 您是否在视图页面中添加了 @Model.namespace.EventsHelper?如果不是,它将无法工作,请在此处粘贴您的视图以识别实际问题
  • 我得到了同样的 ArgumentNullException。是的,我确实在我的视图中添加了正确的模型路径
猜你喜欢
  • 1970-01-01
  • 2012-10-12
  • 1970-01-01
  • 2021-04-26
  • 2020-06-20
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-05-29
相关资源
最近更新 更多