Saturday, 14 November 2015

CRUD operations in ASP.NET MVC 5 using C#


  • This article is for beginners and it's for performing the CRUD operation in ASP.NET MVC 5 using C#. And in next article we'll see Searching, Paging and Sorting a table
  • Note :Download the Visual Studio 2013 or higher so you can grab all the latest features.
  • First create a new project in Visual Studio( * in this article 2013 is used) and give appropriate name to it.

  • Now after clicking OK one new window will prompt, where it will ask to choose the template. Simply select the empty template and check the MVC as shown in below picture.
  • Now after clicking OK, blank ASP.NET MVC solution will be created.

  • Before moving forward you need to create a database in Sql Server. I've created a database named TestDB and a table EmpDetails that contains EmpId, EmpName, EmpAdd, EmpNumber field.


  •  Now for connection of MVC to database we'll use Entity Framework . So we'll goto Tools-> Nuget Packet manager and install the Entity Framework package in our solution. After installation you will see this kind of view.

  • Now we'll add the Model(i.e. Class.cs) in Model Folder. Let us name it as EmpDetails.cs. And add namespace :
    1. using System.Data.Entity;
    2. using System.ComponentModel.DataAnnotations;
    3. using System.ComponentModel.DataAnnotations.Schema;
        • Now mention the table name and key as shown in Class file as per your need.



        • Now add a connection string to the web.config file as shown and make sure that the connection string name is same as of DBContext Entity.... here in my program it is EmpDBContext.




        • Now model has been created and we'll create a controller for action.But before creating the controller you've build your application once to effect the changes. So, here's a MVC 5 scaffholding part where you didn't have to do any code scaffholding will do by itself. You just have to do is select the controller by going to Controller Folder-> Add -> Controller-> Select MVC 5 Controller with views, using EntityFramework.
        • After selecting , window would be prompted which would ask for Model Class select your Model Class, then select the data context. And controller name would be taken automatically. Click Add. The scaffholding process would be going on which will create one controller named EmpDetailsController.cs in Controller Folder and simultaneously it will also create a View for EmpDetails controller where you would see the Create, Delete, Details, Edit and Index.cshtml files in Views-> EmpDetails folder as shown


        • Now just goto->AppStart->RouteConfig.cs and change the controller from Home to EmpDetails and run the applications.
        • You will get the output where there would be one table and you can Create , view delete the data of the table.
        This is how an easy to develop  Application in MVC 5 is done and is quite simpler. For understanding the details code you can go through class file generated in Controller. In next article we're going through that. Have a nice development.





        No comments:

        Post a Comment

        How To Pass An Array As A Parameter While Calling an ASP.NET Web API C#

        Please visit my C# Corner Blog for this, where I have provided better efforts to make this concept more understandable - www.c-sharpcorner....