How to save List or Object in Session in ASP MVC .NET 7 | Session in .NET 7

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
Hello friends how are you in today's session I will explain how we can save a list of objects or how we can save a single object in a session in spmvc.net 7 so let's restart I will open the visual studio uh 2022 Edition right and then I will create a project in spmc.net 7 so click on this option and I will search for spmc .net okay now from the template select this one sp.net core web app with the model view and controller option right then click on next and here you have to give the name to your project so my project name will be session in session dot dot net okay it will be the project name and solution name will also be the same as project name for the time and I will give here a location to my project so it should be like this way folder session in s yeah for you guys yes no select folder and click on next from the drop down you have to select the dotnet 7 and keep this option as check from the authentication type you can use the built-in individual or any other type but for the time I will select none and click on create okay so was the project created successfully then I will show you how we can save and use a list of object in spmsc.net 7. okay so the project created successfully now let me run the project okay so you can see there's a project running successfully now let me go again to the visual studio I will stop at the time and here I will add two classes one class will be the categories I will set these categories list in a session okay categories and here let me add some properties like black end it degree ID okay lets me add here uh degree name and also I will add here is active okay now let me add one another class users okay and I will add here some properties public int user ID here I will add one another property with the name first name first name and the end is active and it will be a string property let's make up with this one and also pass for last name and email and it will be a Boolean property okay and here let me also keep these Pro this is a string now let me go to the controller to the home controller and here when somebody click on the Privacy I mean page if somebody go to the Privacy page then I will save here the data of the categories and users in a session right now let me create here a list of the categories first okay and here I will give an m a uh it will be the category categories okay new then I will create a single object of the user okay here like in these users is equal to U here lets me add some data to the categories so categories dot 8 degrees okay the category ID should be one category name should be Electronics the is active should be one I am just creating some some I am only adding some items to these categories list okay so it should be true okay now let me add one another row and here the category ID should be to the category name should be close and here it should also be active and lets me add one another category with the name like box okay and it should be false now I will use these categories in a session and I will then use in my application right now here lets me also add some data to the user object user Dot user ID should be one the DOT first name should be the dot last name should be so no let's suppose for the time and user dot is active should be okay I am just uh I mean preparing some dummy data now let's suppose you get these data these list of categories from the database and then you want to store it in a session uh so that you can't make uh again and again a call to your database so I will show you how you can save these lists in a session and let's suppose you um get the login user data from the database and then you want to store these complete object uh in a session then I will show you how you can do this okay now here first I will save the categories list in a session save categories list in a session okay no to use the category in Sp mvc.net 7 you have to add some kind of setting in your program dot CS class so I have just copied the setting from one of my other project so that we can save our time now here in the Builder dot Services just add here the builder.services.ad Singleton and use these class and namespace oh sorry interface right and then you have to add this configuration also I will explain this okay it is one time activity you have to add this kind of thing in your program.cs so just press enter and at this place you have to you to add in the services the air distributed memory cache okay and then in the next line you have to add the session in the services like in this way here we are specifying the times uh let's suppose I give here 10 minutes only then my session will be expired after 10 minutes and if I want the session to remain for an hour then it I will give I will give here 60 minutes okay so you can give here minutes though you can also remove this one it's just for I mean to divide the area okay so I will remove this at the time and final thing is you have to add the app.use session here okay just add these middleware app.use session and then go to your controller again to the home controller control s and here you have to uh use the dependency injection so just copy this line and you have to use the I HTTP context accessor interface for accessing the session right no also um see it in the Constructor of the home controller so control tab I edit directly using the uh lessons that gives by the visual studio okay now copy these entity I mean this uh object of the HTTP contact context accessor uh interface and come here in the Privacy page you can add this code anywhere in your application for storing the session that you require for the time I am using the section method so I will show you how I can call this method okay so HTTP context Dot HTTP context Dot quotation dot set string right okay and then just give here a name to your session variable so my session variable name let's suppose will be categories here I will pass this object but keep in mind that these function will only save a string in a session but here it is a list of categories so I am not here this is also show that cannot convert from generic session list to string okay so I will first convert this list uh to a string and then I will save in a session so for performing this activity lets me add here a new get package right click on dependencies nuget package and search here for soft Json this Library okay now install this Library into your project once this Library install then I will show you how we can say uh I mean do the remaining activity okay so the package installed successfully lets me close this window and here I will convert as I said the list to a string and then I will save in a session okay so let me declare here a variable of tab string it degrees and I will type here Json convert Dot serialize object and here I will pass these categories list okay now first I will convert the categories to a string and then I will save here in a session the category string right now here in this place I will also perform this kind of kind of activity first I will convert this object into a string and then I will save in session no user data in session and here context if I show you the context it is this interface variable so it is the uh I mean built-in interface if I just go to the definition it is existing in the microsoft.sp.net code.http name space right I will use the context.http context dot session dot set string okay and let me give here a name to the string [Music] um there's user data okay it will be the variable name and here I will convert this user entity to a string first string users okay and here Json sorry guys Json convert Dot serialize object and here I will pass the user let me also remove this s from the variable name okay user okay now I will save this variable in a session it will be a string variable okay now once I save the session in this place then I will use in some other controller in my application so for doing this lets me add one another controller with the name let me give here a name location demo controller okay now I am saving uh the session inside the home controller privacy method and I will use inside the session demo index page but let me add here one another method like uh demo underscore one okay so here I will access all the session variable what once I created so now let me go to the uh yes it is my first session variable and here it is my second session variable so I will access this variable here in this place okay now let me try uh degrees from session okay and here in this place I will try achieve the uh object from session okay no let me create here a list of categories first we controls it degrees of categories okay and here at this place lets me get the data from the session so I will get like in this way sorry first lets me copy this interface this one Ctrl C and here I will inject it in the Constructor of this class so click copy the name of the class session controller okay and here inject the interface sorry like not like in this way just copy this one text will be equal to context okay and now I will access these object context dot HTTP context Dot session dot get string okay and here I will just give you the name of the string so Ctrl Z sorry ah I have just to I mean give here the session variable name but here I am getting an error that string to the list type right now here lets me perform an activity one extra activity Json convert dot d serialize object okay and here I will give a type so I want to to convert these Json variable into a list of categories so Ctrl C so Ctrl sorry control Z control C control V and here I will copy this complete line okay Ctrl X and passed here right now you can also first get just these variable in a string like in this way lets me uh thing um categories string okay I am just getting the categories list string from the session and then I will pass here sorry controls it here and here I will convert into the categories okay now here in this place I will also write the same code for the user object no copy this one and passed here I will just get the user data from the session so it would be the thing okay so let me also declare here a variable for the users user okay and here I will save the user data from the session and here you can see that uh it's giving me an error cannot cannot implicitly convert the list to the session or to to this object okay because it is a single object so I will remove here the list and I will also pass this class name users okay now let me put here a debugger and lets me also put here a debugger okay so we can see how we can getting the data these lets me add a page add View it and I will add here and name to The View demo underscore one eight okay no let me show it here in h tag demo one control s and run the application okay now as I write the complete code for uh saving the list and the object data in a session here in the Privacy method so I will click on these uh option and let me show you here the process so step forward next next next no I have added three items to the categories list you can see here the first one the third and the last okay no hey at this line I will convert these lists to a string first step forward let me show you here the string you can see that all these data converted in a Json string if I show you in a notepad okay so it is basically a string of Json you can see here it is a Json string okay now lets me click on the on these options step over know that after executing this line these string is saved successfully in in the session variable with the name categories now step next next let me show you the data of the user no here it is the data of the user okay and let me also convert into the string if I show you the string of the user it is a Json string you can see the user ID the first name and other thing and let me save also in a session no continue okay so the session data of list of categories and user saved successfully now to check whether it saved successfully or not let me navigate to these function of the session controller because I am getting the session data again back from the session in these function no I will navigate to this function demo underscore one okay no the method heated successfully lets me step forward I am getting uh the session no in the string variable okay let me show you whether it's working okay so I get it the exact data from the session that I store and now I will convert into a I mean into the list of categories and here you can see that I am getting the exact data and no I will also get the user from the session you can see here continue okay now let me also display you here the data of the categories and user in this page so I will return these categories and user object to the view so let me add here a class stop the application okay this made here a new class command model dot Cs and inside the common model I will declare here a list of categories okay and I will add here an object of the user okay lets me declare a variable of this class inside the demo controller [Music] to the demo controller sorry session demo controller right and here I will declare common model is equal to model dot new model and in the model dot list categories list okay I will assign this variable this list of categories and in the common model dot user I will assign the user data right and then I will return this model to The View now go to The View and here lets me declare here the model is common model so let me copy the namespace okay go to the demo.1 page again so it will be my model s dot common model right and here lets me I mean declare here a table and inside the table I will show you the list of categories okay table th okay lets me add one table header okay um it will be the degree ID or c TRL V category name is exactly okay here lets me add a table body T body okay and TD sorry TR and here in the table row lets me add some data uh first I will add here a table row here let me add these all option Ctrl X control V can control the like in this way sorry guys so let me close this one okay Ctrl k d okay and here I will use the for each Loop to display the categories a variable items in model dot categories okay let me copy these one and paste here control sorry let me copy uh here let me add one TD table data the first one will be the item dot category ID the second will be the category name and the third will be the is active option so category name is active okay and let me put this table in a in a format so I will declare here a class row and inside this div let's make two two other divs the first one will be the clause LG uh at let me add here one another class sorry one another div okay and copy this one Ctrl X it should be like 12 and it should be like at Rolex Ctrl V okay and let me display here the user's data okay laid here a UL item item uh would be um model dot users Dot first name and here let me also add the last name model dot user dot last name okay and in the second line I will display the is active option okay let's remove this code is active okay now let me run the application again Gandhi privacy okay and here I am saving all the categories data in a session and now I am saving the user data in a session continue okay now I will navigate to the this page okay so it is in the session demo uh and demo and demo one okay I am getting these data from the session I am getting user object from the session and then I am saving inside the user and categories okay let me return this model to The View click on continue and here you can see that the categories list display successfully and here I am also displaying the user object data in a view so you can use the session in.net same like in this way you can use it to store the list of objects you can use it to store the a single object and even you can use it just to save a string or an integer value in dot net 7. thank you for watching the video and don't forget to subscribe the channel so that I load more content for you thank you
Info
Channel: Noor Codelogics
Views: 5,662
Rating: undefined out of 5
Keywords: session in mvc, session in net core, netcore session, aspnetcore session, session c# mvc, dotnet core session, mvc core session, session net 7, use session in .net core, how to use session in asp.net core view, how to use session in asp.net core mvc, how to store value in session in asp.net core, session in asp.net core, session in .net 7, .net 7 session, session .net 7, how to save List in session .net core, how to save object in session .net core
Id: 6bPeFO10GN4
Channel Id: undefined
Length: 33min 37sec (2017 seconds)
Published: Sun Mar 19 2023
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.