我正在创建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 with Index action
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对象不为null并且如何修复它,我不明白为什么会出现此异常
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();
}
}
SocialEventsContext
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
答案 0 :(得分:0)
将属性添加到模型类中,该属性将填充控制器,然后传递该模型以进行查看。
型号
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 }