Accessing Data in MVC Using Model

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
[Music] hello welcome to Norwich technologies this is Sudha Sharma in this session we are going to continue our previous video session concepts entity framework database first approach to continue with this video you need to watch our previous session video how to create the poco classes for the database existing in the previous video session we discussed about how to use the database first approach where all the domain-specific classes are created according to the database and tables available now we are going to use those poco classes to interact with the database we are going to create a simple UI in MVC that will access the data from the tables and use the data here we are using the database first approach through data base first approach we added an entity data model this is our entity data model Northwind DB model and this entity David data model is having the model classes TBL category and TBL product category is mapping to the table categories and product is mapping to the table products now we have a context plus here that is Northwind eb entities this context class Northwind eb entities is having two DB sets TBL categories and products to interact with the categories table and products table now let's see how to interact with the data how to get these classes that we already discussed in our previous approach database first now we are using these classes to communicate with the database so let's see how we can interact with the database and tables after adding the idea dotnet entity data model for the products and categories tables we are adding a new controller let's add a new controller and name the controller as products controller it is an MVC 5 empty controller we are naming that controller as first we will use categories categories controller will have the actions which are responsible for accessing the category details so we are creating a categories controller and in the categories controller we need to access the model classes to access the model classes I am importing the name space using entity framework DB first models because this DB first models namespace is providing all the classes to interact with the database and in the categories controller we are creating north wind DB entities we are using north wind DB entities reference DB is equal to new north wind DB entities through this DB reference you can access both the categories table as well as products the categories controller is having an index action I want this index action to return the list of all the categories so how we can return the list of all categories categories are accessible by using the reference of DB so in the view we are accessing DB categories table can be accessed by using a DB set called TBL categories and I want to return the list of all the categories so we are making it as a list in the categories controller there is a index action this index action is accessing the list of all the categories and returning to the view now what we want to do here in the view is loop through all the categories returned by the index action and print the list of all the categories let's see so we will add a view for the index section right click on index section and select the option add view it's prompting to define the view name let the view name be index and the template we will select as empty and with the model class it must use the model class TBL category because we are accessing the data from category stable and we are using a DB set which is of type TBL category so we define the name as index and template empty and model class as TBL category now let's add this view in the view you can observe the view is now strongly binded with the table TBL categories what we have to do in the view is loop through all the categories and print the list of all categories so that user can view the list of all the categories available in the categories table however here one important point is you are adding a strongly type of view and the strongly type of view you can observe it is defined for the TBL category but our index action in the controller is the sending a list of all categories then we need to convert this into the list type means an i enumerable we are using an ienumerable to designate this as alice type so now model is having the collection of all the categories we need to loop through all the categories present in the model and print them here so let's put the heading as categories index and in the categories index we need to print the list of all categories then we will use an ordered list to loop through all the items inside the model we use a for each item in action collection is model so we are accessing every item through this model and we want to print that as a list item and we will access item and we want only the category name to be printed then we will access the category name field that means the categories list is passed into the index view we are collecting the list through a model we are looping through every item inside the model and accessing the category name and displaying as a list item initially let's run this and see how it looks like when I access the categories controller index action I will get the list of all the categories available inside the categories table so here one important point is we are not writing the classes we just generated the classes by using entity framework DB first approach generally if you have the database and tables ready then you can generate the Poco classes by using those proko classes you can communicate with the database so here exactly we are doing the same we are using an entity model that generated all the classes by using those classes we are interacting with the database so in the categories controller index action it is a it has to return the list of all categories let's access the categories index so in the URL we are entering the controller name categories and the action name index and if all the connections are defined properly and configured properly then it has to get the list of all the categories and display right now you can see the list of all the categories available and displayed here so we are accessing the list of all the categories and displaying in the categories index but we want these categories category names as hyperlink so that whenever user clicks on a specific category I need to get the list of all the products belong to that category then how to designate this list item as a hyperlink so in MVC to designate anything like an hyperlink we have an helper method here the list item instead of displaying some literal text we will use an HTML action link action link will render an HTML anchor tag in the actual link we have to define the link text I want to get the category name as link text and display then I will access the category name as item dot category name so you are creating an hyperlink where the link text will be item dot category name that means whatever the category name is there it will get the category name and generate as an hyperlink what we want this hyperlink to do is whenever user clicks on any specific category we want the user to write redirect and view the list of all the products belong to that particular category so that means now this action link will have the text category name and it should redirect to an action called index in a controller called products that means when user clicks on the link that will be redirected automatically to an index action in products still this index products is not yet ready to interact so we need to design that index products so initially we just made our category names as hyperlinks so that whenever user click on that hyperlink he will be automatically redirected to the index action in the products controller but very important here is whenever user is redirected to index products we want to display the details of the products that belong to that specific category that means if user clicks on electronics I want only electronic products to display and user clicks on shoes I want only shows product to display in that situation when you are redirecting from the category to the products we need to pass this category ID into the products so that you can filter the products based on the category ID as already we discussed in our previous session while creating the database table we defined a category ID as a foreign key in the products table so whenever user clicks on any category we will redirect to the index of products and we will pass the current category ID into that index so that the products index can filter the list of all the categories belong products belong to that particular category so here we need to pass category ID as a parameter then we have to pass as new anonymous object and a new anonymous object the parameter name is ID and it should pass the category ID into it we are passing the category ID and whenever you are creating the links in MVC it's always important that it will not pass any default parameters then you have to define it to know so now when we run this categories index action you can observe the categories index action this time it is having the list of all the categories displayed as hyperlinks so that user can click on any category and get the list of all the products belong to that particular category so you can see in the output now we are having the categories table categories index in the categories index we have two categories electronics and shows when I click on the electronics I need to get the list of all the electronic products and see what happens when I click on this initially it shows 404 status code that's reasonable the cause there is no products controller we created we will create that now and see it is expecting your products controller with an index action and it is passing an ID into that when I click on and see the ID is 1 that means the category ID 1 and when I click on the second category issues then you can see it is passing the category ID 2 so based on that we need to filter the details in the products and display the details of the products so what we have to do when user clicks on the category I have to return the list of all the products belong to that particular category so let's do that now we will go to the controllers in the controller's already the categories controller is ready with categories list we will add one more controller and let's put the controller as empty MVC controller with the name products and in this products controller you can see we have an action index let's define the models here using entity DB first dot models so that I can access the model classes we need to create a reference for the context that means north twin DB entities DB is equal to new north wind DB entities so this reference will access the list of all the categories and products here we want the products and we have a controller called products this controller products is having an index which will return the list of all products but exactly we don't want to return the list of all products we want to return the list of the products that belong to the category that user click earlier in the previous screen that means here the index action is expecting and parameter ID so we are passing the parameter ID and what the ID must be it will it must be the category ID so in the previous whatever the category user selected that category will be passed into that index action now we need to check whether that particular category related products are there or not in this products collection so how do we do that we will not use any database queries here we will use a simple link you query for that so DB dot and we have the list of all the products that can be accessed through TBL products in TBL products we need to search for the products that are matching with the category ID then we have to use the link u third any-any based on which field you are using you can access by using any or you can also use the link u statement where so just you have to check whether the products belong to the specific category or not and filter the list and display so we can use any or we can use where so here we have we want to return the list of all the products then better option is to go with the where so DB TBL products where the category ID is matching with the category ID how do we access the category ID X implies X dot category ID is equal to whatever the ID you pass that means it will verify whether the category ID you pass is matching with the category ID field inside the products table if any product belong to that particular category then it will get that particular product and the return it to the list that means now what index action is doing it is expecting the category name whenever invoked and when the category name is possible it will verify whether that particular category related products are there in the collection or not is they are there and it will return the list of all the products now if when it is returning the list of all the products we have to restore in a list type we'll give the name as products list of it will be TBL product type and I will give the name as products and store that collection so whatever the products returned by the index action now those products are available in this products reference now we want our view to render that products details then we have to pass that products into the view that means whenever the index action is called the category ID is passed into the index action it will verify whether any product is there by that particular category ID or not if there are products belong to that particular category then get the products and store in a products reference this product referent is passed into the view so that now we can use all the products in the view and display them so let's add a view for index action I will add a view view name we will define as index and template it will be again empty model class this time it belongs to TBL product product ok let's add this view now in this view again you are sending a list of all the products that means you have to convert this into NI enumerable we are converting this into an ienumerable and now the list of all the products are accessible with the help of model let's put this as a products list okay now how do we loop and print through all the products we will use a simple for each wire item in collection the collection is model this time we want to print only the product name what are all the products belong to that particular category then as a list item we will access item dot name because we want to print only the product name but an again important point is this product name must be like an hyperlink so that I click on that particular product and I can get the detail of that particular product first up to this point let's see whether it is able to get the list of all the products belong to that particular category or not then we can extend this code so I will start with the categories index action in the categories index action it will display the list of all the categories available and it will show the category names as links so that user can click on any category link and he can get the list of the products belong to that particular category so in the categories index page in the categories index page we have two categories one electronics and shoes let's click on electronics and see now you can observe it will have to get the list of all the electronic products and so it has to get all the electronic products and so now similarly now I will go back and click on shoes it has to get the shoe products and display so now the navigation is properly working we are able to see the list of all the electronics and products separately but I want this products as links so that I can see the detail of every product now so how to achieve that so what we will do is in the products controller we want to display the detail of every product again then what we can do is whenever user views the list of all the products here you he is viewing the list of all the products we want this as hyperlinks again then we will use an HTML action link link text will be the product name then I can access that by using item dot name and when user clicks on that particular product name he must be redirected to an action called details the details action is expecting the product ID so that means we need to pass the product ID so that it will display the details of that particular product so we will pass as new anonymous object and it is expecting an ID ID is the current product ID you can get that by using item dot product ID we are passing the product ID into the details so what this index action is expecting is whenever user clicks on that particular product it will be showing the details of that particular product and how do we get the details of that product let's see so that means whenever user clicks on the product name it is redirecting to end details action and it is passing the product ID so in the products controller we need another action we are creating an action result by name details into the details action we are passing the ID this time this is a product ID we need to verify whether this particular product ID is available or not so if it is available then get the details of that particular product and display individually every product information so what it should return it should return a view and in the view it should return the detail of that specific product that is matching with the ID so DB what is the table name TBL products in that we have to search for a single record where the product so that means the product ID is matching with the ID we pass that means X dot product ID is equal to the ID you match that means it will search for that particular ID product and it will return that product to the view so let's add a view for the details action and add a view for the details action view name is details and the template is empty the model class will be again the TBL product so let's add this this time it is not returning the list it is returning only one single product information then we will put the header as product details and we want to display the details of all the products in a tabular form let's create a table in the table we will add the table row first cell it will display the product ID and how do we access the product ID and display we have to use model and the Product ID model dot product ID similarly we need to display the product name and price I will copy this code and change this into the product name this time the model is expecting name and one more row with the details of product price and let's add the product price price okay now let's run this and see that means we are displaying the details of all the products individually so first we will start with the categories index so when we get the list of all the categories user can click on any specific category so that it will return the list of all the products belong to that particular category user can view the products and select any product so that he can view the details of that particular product that's what the UI we have designed here so the first categories index will come that shows the list of all the available categories in the categories table and user clicks on any specific category like electronics or shoes when he clicks on electronics it will show the list of all the electronic products as hyperlinked and when user wants to see the detail of any specific product he will click on any product like TV and he will get the detail of that particular product now this is perfectly working but we need a navigation proper navigation mechanism for the website because after watching the details he can go back to the products list from there he can go back to the details right let's see how we can do that that means from details action user must be redirected to index so in the details view in the details view after displaying the table here we will add a line break and an action link HTML action link the link text will be like back to products products list and products list is available in an action called index but index is expecting the category ID then we have to pass the new anonymous object ID ID is the category ID that you can access by using model dot category ID that means we are passing the category ID into that so that it will get back to the list of all the products now user can be navigated back from the details to the products index so this is the products index where it is displaying the list of all the products now from here user must be able to go back to the categories then here we will add one more action link action link this time this action link will have the link text as a back to categories so that it will be redirected to an action called index in a controller called categories so let's start with the index action and see now we have a proper navigation mechanism between these views so that user can be navigated from one view to another view easily so first it will get the list of all the categories available through the index action from the list of all the categories user will select any particular category to view and he clicks on any category like shoes it will get the list of all the shoes now from here user can go back to the categories by clicking on this link it will get back to the categories user can select any particular category and from there user will click on any particular product it will get the detail of that particular product and from there user can switch back to the product list product list will get the list of all the products and back to the categories this is one simple example of how do we consume that database poco classes and interact with the database so in this example we created a simple UI to interact with the tables that we created through code first approach through code first approach we created the tables through database first approach we imported the classes and now we are using those classes to communicate with the database in tables if we can understand these two approaches now we can see the third type of approach how the model first approach works that we will see in our next video session thank you [Music]
Info
Channel: Naresh i Technologies
Views: 7,797
Rating: undefined out of 5
Keywords:
Id: hQ19fVb-SmQ
Channel Id: undefined
Length: 30min 9sec (1809 seconds)
Published: Mon Dec 26 2016
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.