I am migrating project from Net MVC to MVC Core 2.
How do I set IQueryable in Sessions? In Net MVC it was the following,
public ActionResult CurrentOwners_Read([DataSourceRequest]DataSourceRequest request, int propertyID)
{
if (propertyID == 0)
{
throw new ArgumentNullException("propertyID");
}
IQueryable<PropertyOwnerRoleViewModel> allResult = (IQueryable<PropertyOwnerRoleViewModel>)HttpContext.Session.GetString(_currentOwnersResult).AsQueryable();
if (allResult == null)
{
PropertyOwnerManager propertyOwnerManager = new PropertyOwnerManager();
allResult = propertyOwnerManager.GetPropertyOwnershipSummary(propertyID).AsQueryable();
Session.Add(_currentOwnersResult, allResult);
}
Last Line above is giving Error:
The name 'Session' does not exist in the current context
_currentOwnersResult is string AllResult is IQueryable
When trying to convert in MVC Core, the following does not work either
HttpContext.Session.SetString(_currentOwnersResult, allResult);
Error Code:
cannot convert from 'System.Linq.IQueryable<HPE.Kruta.Model.PropertyOwnerRoleViewModel>' to 'string'
Okay, so as a basic example on how to setup a complex object in your Session in .NET Core:
First setup your session:
In your Startup.cs, under the Configure method, add the following line:
And under the ConfigureServices method, add the following line:
To add complex objects in your list to your Session (Please note that this an example only and not specific to your case since I do not know what PropertyOwnerRoleViewModel definiton is):
Model:
Then create a SessionExtension helper to set and retrieve your complex object as JSON:
Adding this complex object to your session:
And finally to retrieve your complex objects from your Session: