TECHNOLOGIES
FORUMS
JOBS
BOOKS
EVENTS
INTERVIEWS
Live
MORE
LEARN
Training
CAREER
MEMBERS
VIDEOS
NEWS
BLOGS
Sign Up
Login
No unread comment.
View All Comments
No unread message.
View All Messages
No unread notification.
View All Notifications
Answers
Post
An Article
A Blog
A News
A Video
An EBook
An Interview Question
Ask Question
Forums
Monthly Leaders
Forum guidelines
Adalat Khan
647
1.5k
870k
How to Pass Model to Session in MVC Core
Jul 7 2019 7:22 AM
I have an application that has Role based login system. In Login View when we press the Login button an Action Method is called that retrieves the User Righs of the current user from the UserRights table and assign the query result to the Model class MenuModels. I want to assign the MenuModels class in to Session to create dyunamic menu for the current user but the Session does not work in MVC Core. Fiollowing is my Code:
//Get the User Rights Assignments or names of the Sub Menu Items assigned to the current selected user from the UserRightsDetails table joining with the MainMenu and SubMenu tables because the UserRightsDetails table only contains IDs of the Main Menu //items and Sub Menu Items
IEnumerable<MenuModels> menuDetails = (from userRights in dbContext.UserRightsDetails
join mainMenu in dbContext.MainMenu
on userRights.MainMenuId equals mainMenu.MainMenuId
join subMenu in dbContext.SubMenu
on userRights.SubMenuId equals subMenu.SubMenuId
join uRole in dbContext.UserRoles
on userRights.RoleId equals uRole.RoleId
where userRights.RoleId == userRoleId
select new MenuModels
{
MainMenuId = mainMenu.MainMenuId,
MainMenuName = mainMenu.MainMenuName,
SubMenuId = subMenu.SubMenuId,
SubMenuName = subMenu.SubMenuName,
Controller = subMenu.Controller,
Action = subMenu.Action,
RoleId = uRole.RoleId,
RoleName = uRole.RoleName
}).ToList();
//Pass the Model data to Session
HttpContext.Session.SetSessionModelData("MenuMaster", menuDetails);
The SetSessionModelData is a function that i declared inside in the SessionExtentions class and i defined this class in the same controller where i used the above Linq Query. Following is my SessionExtentions class:
//Declare a Class to define a method that set and get the Complex data into session
public static class SessionExtensions
{
public static void SetSessionModelData(this ISession session, string key, object value)
{
session.SetString(key, JsonConvert.SerializeObject(value));
}
public static T GetSessionModelData<T>(this ISession session, string key)
{
var data = session.GetString(key);
if (data == null)
{
return default(T);
}
return JsonConvert.DeserializeObject<T>(data);
}
}
when i use the the GetSessionModelData() method in another Controller then it gives me the following error:
Newtonsoft.Json.JsonSerializationException: 'Cannot deserialize the current JSON array (e.g. [1,2,3]) into type 'HRandPayroll.Models.RetrieveDataModels.MenuModels' because the type requires a JSON object (e.g. {"name":"value"}) to deserialize correctly.
To fix this error either change the JSON to a JSON object (e.g. {"name":"value"}) or change the deserialized type to an array or a type that implements a collection interface (e.g. ICollection, IList) like List<T> that can be deserialized from a JSON array. JsonArrayAttribute can also be added to the type to force it to deserialize from a JSON array.
Path '', line 1, position 1.'
Following is my call of Get method:
ViewBag.LoginSession = HttpContext.Session.GetSessionModelData<MenuModels>("MenuMaster");
This call give me the above error message of deserialization. Please give me any idea. Thanks
Reply
Answers (
8
)
3Des with 32-bits key in asp.net
Switch from technology