Asp.net Mvc : Create Update Delete View With Entity Framework

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hi guys welcome to coordinate move in this video we will discuss how to create a simple MVC application with code operation course stands for operations like create retrieve update and delete let me create a fresh MVC application for that go to file then new project under wishes eShop select web than asp.net web application name your project here instead of the location where you want to save the application click on okay the select MVC then keep others as it is click on OK so here we have our brand new industry application now I am going to do the DB part for that I will be using management studio let me come to my SQL Server instance and I am going to add a new database for that right click on databases new database name your DB here it will be M little code in my case click on OK then inside the database I am going to add a table for customer for that right click on table then new table and the column details here ID int and name for customer name work 50 then description as well 250 then I'm going to set primary key for the ID column and I'm going to specify the identity specification for the column for that set it to yes so we don't want to insert values into ID column SQL Server will take care of that upon new recording session its value will be incremented by one so now we are going to save the table for that you can press ctrl s or you can click on the Save button here name your table as customer click on OK if you refresh the tables folder we can see the new customer table the less frequent official studio now we are going to add ad or dormant entity model of the database into models folder for that right click on models then add new item select data from the left panel then ad or government entity data model then name your entity model here in my case it is DB model click on app select delete from database click on next click on new connection now enter USGS / instance details here let's select the DB here in DC code click on OK I want to change this into DB model click on next then select the double customer table then click on finish now you can see the entity diagram of our fabric customer table inside the models folder you can see customer dot cs5 inside that you can see a class customer so you can see properties respect to you our customer table ID name and description so we will be using this customer plat for our model purpose now I want to add customer controller into controllers folder for that right click on control us then add controller I am selecting MVC 5 controller v3 door right actions econet namely control as customer controller we connect so here we have the customer controller it contains actions for core operations from now onwards I will be using MVC scaffolding mechanism to create views and for DB operations I'll be using entity framework inside the model class if you look into the DB model got context or 3s file you can see because DB models we will be using an object of this class to insightly DB now back to controller if we navigate to customer /index the request will come to this index as so inside index we'll be listing all the customers that are entered into the customer table so first of all we need to use the namespace of our model for that MVC code dot model will be the namespace and then inside the index action we'll be training all the customers like this do the models equals mu DB models then we'll be punishing the customers list into this view DB model dot customers to list now we want to create the view or index for that right click on index then add view the new ste our list template so it will create a list of customers and then we'll be using the model customers and then click on add now let me run the application for that hold control then press f5 now let's navigate into customer index section that's it it is bad symbol here we have the table to list all customers in our tab currently we don't have any customer so there is no focus mo next we will be creating views for creating customers the last part of the video we will discuss how this scaffolding mechanism and HTML helper classes works here we have the action creat to create new customers we have to navigate into customer for / grade so this action should return a view to create new customers for that you can right click on the create then add view then select template as create and select the proper model class as customer then click on app create view is created now let's give the solution we know the page now let's navigate to customer /script or otherwise you can click on this grade new it'll also navigate to create action so we have the form here to add new customers so if we enter and click on create button the data from these two text boxes will be sent to HTTP POST action so here we have the HTTP POST create action so we need to save the poster data into our customers table for that you can do something like this using DB model GB model equals new DB model then DB model door customers instead of this form collection object we can replace it with customer so givi model dot customers dot and so will be adding the poster customer details i call the Save Changes matter bill dissolution we load the page now try to enter some customer details here customer one description click on create that's it so here we have the entered customer details here actually no we are in index action do you know why after saving the customer details here you are redirected to index action by calling or your direct to action method now if you check the customer tab in our DB you should see the newly added customer there also next we are going to cover the details part here you can see a hyperlink with text details and if you check the inspect of this link you can see that it's a chav is referring to the details action in our customer controller and also it is passing the customer ID one to the action so if you check our controller you can see the details action here and we have the ID as parameter inside the action we need to retrieve the specific customer with the e1 customer ID so for that you can do something like this using BB models DB model equals u DB models and you can put this what can you inside using DB model dot question dot will X that X dot ID equals leave an ID force or default okay now let's create the detail view for that why pick on details that add view select details embed from here and select the customer motor class they can add so here we have our new details view now let's build the solution click on this details hyperlink that's it here we have the details of customer with customer ID 1 now back to the list in group operations we implemented create and retrieve operations now we want to implement operations like edit or update and delete operation so next we are going to do the edit operation here you can see a hyperlink with text edit it will take us to the Edit action now here you can see to edit actions one for HTTP GET and one for HTTP POST if you click on this edit hyperlink the because will come to this HTTP action and from this action we can perform to edit the particular customer with given ID and after changing the customer details you see submit the form the form will be posted into this HTTP POST ad ash from the after AT&T customer details we will be redirected to index action so that we can see the updated customer list now let's do that let me create a view of Eddie for them quite click on edit then add view keep the name as it is and then change the template to edit then select the customer cause then click on add that's it here we have the Edit view so from the Edit action we need to pass the particular customer for that let me copy the same code from details and fishing here since we need the same code I just copied the code from our details action now inside the edit post action you can update the customer it is like this will be model delete model equals new DB models now in entity framework if you want to update you can do something like this maybe model got angry and passed the particular sorry instead of this function we need the customer model object so he can pass this customer and then we will change state two entities tail for that you need to import using systems or data dot entity to add missing main spaces we just need to add using directly here whoo you can't physicals on it and whole candles and : you can see the possible namespaces from there itself you can use all the missing things pieces okay indeed estate dot modified then just call the Save Changes message that's it we will be redirected to index action after updating the customer so let's build the solution click on edit yes here we have the custom I edit from here if I want to change the customer name from customer one to customer 100 I would do that here and then I click on save and it is updated to custom 100 and here we have the updated custom on this here now let's do the final delete operation in code application for that we have one HTTP GET action here for delete and one post action for delete and first of all we are going to deal with the get request for that will be copying the same or quote from the edit the only difference will be there in the view so now we are going to create the view for delete for that right click on delete then add view select delete em brain then select customer as mona cause well click on ADD build solution click on delete so here we have the response from HTTP delete action now if we click on delete now it will send to the delete post action and we have to change the delete HTTP POST tension to delete corresponding all customer with given ID for that you can do using DB models maybe more equals new DB models you want the exact customer question DB model dot customer dot will X says that X dot ID equals u1 ID first 4d 4db sorry English to equal setting DB model dawn customers dot remove this specific customer with given ID after all we call the silkiness function that's it Big D solution now click on the delete button here yes that's it because summer is deleted no so our customer list is empty that's it for today guys we have discussed how to do operations like instead edit delete and view in MVC cool applications in the previous video tutorials we have discussed how to create login page how to create user registration and how to create drop-down lists in MVC applications and much more you can see the related video link in video description and in the topmost command of this video please go to that subscribe to my channel got that move for our some videos like this and don't forget to come into your feedbacks in the comment box please like and share the video with your friends and colleagues so that they can benefit from this have a nice day bye
Info
Channel: CodAffection
Views: 163,343
Rating: undefined out of 5
Keywords: mvc create update delete view, create update delete in mvc, insert edit delete and view in mvc, in entity framework, with sql server database, save select delete in mvc, mvc tutorial for beginners, asp.net mvc insert update delete and view all, crud operation in mvc 5 using entity framework, crud operation in mvc, insert update delete in mvc using entity framework, crud operations in asp.net mvc 5 using entity framework, entity framework database first, CodAffection
Id: QjRLS_Mii9w
Channel Id: undefined
Length: 20min 1sec (1201 seconds)
Published: Wed Mar 08 2017
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.