I'm using Umbraco 8 to create a new website and trying to do something really simple but having no luck.
I'm attempting to use a Surface Controller to render a list of events set up as content nodes. In my controller, I get the results but once they get passed through to my view and I try to access the events i.e. Model.Events.Any() somehow it seems to empty the property of all values.
using Index.Models.Events;
using Index.Models.PublishedContent;
using Papermoon.Umbraco.Kupo.Core.Services.Interfaces;
using System;
using System.Linq;
using System.Web.Mvc;
using Umbraco.Web.Mvc;
namespace Index.Web.Controllers.Surface
{
public class EventsController : SurfaceController
{
private readonly KupoGeneralSettings _kupoGeneralSettings;
public EventsController(IKupoSettingsService kupoSettingsService)
{
_kupoGeneralSettings = kupoSettingsService.GetSettings<KupoGeneralSettings>("kupoGeneralSettings");
}
public ActionResult RenderUpcoming()
{
UpcomingEventsModel model = new UpcomingEventsModel();
model.Title = "Upcoming Events";
model.Events = Umbraco.ContentAtXPath("root/homepage/events/event").Select(x => new Event(x));
model.Events = model.Events.Where(x => x.StartDate > DateTime.Now).OrderBy(x => x.StartDate).Take(3);
model.TotalEvents = model.Events.Count();
model.EventListingLink = _kupoGeneralSettings.EventListingLink;
return PartialView("~/Views/Partials/Events/UpcomingEvents.cshtml", model);
}
}
}
In the code above, Model.Events gets filled fine but when I then do Model.TotalEvents = model.Events.Count() the result is lost. I can show this better in a video: https://i.imgur.com/rE3VAqe.gifv
It's worth noting that I've done similar in a different controller which seems to work fine.
It also seems to do the same if I do a simple check like Model.Events != null
At a total loss, any help would be massively appreciated!
public ActionResult RenderUpcoming()
{
var getAllEvents = Umbraco.ContentAtXPath("root/homepage/events/event").Select(x => new Event(x));
var events = getAllEvents.Where(x => x.StartDate > DateTime.Now).OrderBy(x => x.StartDate).Take(3);
UpcomingEventsModel model = new UpcomingEventsModel()
{
Title = "Upcoming Events",
Events = events,
TotalEvents = events.Count(),
EventListingLink = _kupoGeneralSettings.EventListingLink
};
return PartialView("~/Views/Partials/Events/UpcomingEvents.cshtml", model);
}
If your events returns null, then there could be a problem in your .Where clause. By writing your code this way, you only have to set the model.Events value once and makes for slightly easier debugging.
This will actually get abstracted off into a service but this allowed me to debug so it's probably a bit messy!
This doesn't actually fix my issue however, my where comes back with items just fine but whenever I try to do anything with it, the IEnumerable empties. I can literally be assigning the variable to the model property and by the time it gets there, it's empty.
So, using your example, when I do getAllEvents.Where..., getAllEvents is emptied even though it has values before I attempt the Where.
Glad that you've found a solution and no problem at all.
If you hadn't found the solution, my next port of call would've probably have been to have a further look into the getAllEvents variable, since that was empty in events that'd pinpoint that there was likely something up with the Umbraco.ContentAtXPath("root/homepage/events/event").Select(x => new Event(x)); line.
Accessing a Property Empties It
Hi,
I'm using Umbraco 8 to create a new website and trying to do something really simple but having no luck.
I'm attempting to use a Surface Controller to render a list of events set up as content nodes. In my controller, I get the results but once they get passed through to my view and I try to access the events i.e.
Model.Events.Any()
somehow it seems to empty the property of all values.Here's the code:
Homepage.cshtml:
EventsController.cs:
In the code above,
Model.Events
gets filled fine but when I then doModel.TotalEvents = model.Events.Count()
the result is lost. I can show this better in a video: https://i.imgur.com/rE3VAqe.gifvIt's worth noting that I've done similar in a different controller which seems to work fine.
It also seems to do the same if I do a simple check like
Model.Events != null
At a total loss, any help would be massively appreciated!
Thanks,
Ben
You could try something like this instead:
If your
events
returns null, then there could be a problem in your.Where
clause. By writing your code this way, you only have to set themodel.Events
value once and makes for slightly easier debugging.Thanks Rhys,
This will actually get abstracted off into a service but this allowed me to debug so it's probably a bit messy!
This doesn't actually fix my issue however, my where comes back with items just fine but whenever I try to do anything with it, the IEnumerable empties. I can literally be assigning the variable to the model property and by the time it gets there, it's empty.
So, using your example, when I do
getAllEvents.Where...
,getAllEvents
is emptied even though it has values before I attempt the Where.Thanks,
Ben
Just so I can try set something up locally to replicate this, is
getAllEvents
emptied if you just do:Yep correct, I've had some help over at Stack Overflow: https://stackoverflow.com/questions/55904475/c-sharp-ienumerable-emptied-once-accessed/55904526#55904526
Simple solution is to cast to a list instead of an
IEnumerable
, full details in the above link.Done the IEnumerable thing millions of times in Umbraco 7 so seems like something might have actually changed in U8 to cause this.
Thanks for the help btw!
Glad that you've found a solution and no problem at all.
If you hadn't found the solution, my next port of call would've probably have been to have a further look into the
getAllEvents
variable, since that was empty inevents
that'd pinpoint that there was likely something up with theUmbraco.ContentAtXPath("root/homepage/events/event").Select(x => new Event(x));
line.Glad that things are resolved now though! :)
is working on a reply...