Get Even More Visitors To Your Blog, Upgrade To A Business Listing >>

CRUD OPERATION USING MODAL DIALOG IN ASP.NET MVC



User Id * 156 Title * CRUD Operation Using Modal Dialog in ASP.NET MVC Category * Tag * ASP.NET MVC× Description * In this article I am going to explain how to add, edit, delete and find [CRUD (Create, Retrieve, Update, Delete)] records using razor view engine in ASP.NET MVC3 or above version. Content *In this article I am going to explain how to add, edit, delete and find [CRUD (Create, Retrieve, Update, Delete)] records using razor view engine in ASP.NET MVC3 or above version. It will display modal dialog if the JavaScript is turned on otherwise a simple view page, when we try to add and edit the records of student.

Please follow the steps given below to create an application that allows user to add, edit, delete and find records of student.

Open Microsoft Visual Studio 2010 à Select New Project à Select Web from the Installed Templates à Select ASP.NET MVC3 or MVC4 Web Application à Enter the project name Modal_CRUD_MVC in the Name textbox à Click OK.

After select the project you can see the following dialog box:

Select Empty from the Select a template option à Select Razor view engine à Click OK.


Add the following Modal classes (Student and StudentContext) in your Model folder:

Student class represents the table structure in the database and StudentContext Students property represents the name of table and data of the students in the database.

    public class Student  
    {
        public int StudentID { get; set; }
        public string Name { get; set; }
        public string Age { get; set; }
        public string State { get; set; }
        public string Country { get; set; }
    }
    public class StudentContext : DbContext
    {
        public DbSet Students
        {
            get;
            set;
        }
    }

 Also include Entity framework reference in your project because in this application we are going to use entity framework code first model to add, edit, delete and find records from the database. To include entity framework dll, follow these steps:

Go to Project à Manage NuGet Packages à Enter entity framework in the search textbox and search online.


You will find latest version of Entity framework version.

You also have to configure your web.config file for the implementation of this project. Add the connectionStrings element under configuration tag:

  
   
 

 Add a Controller in your project and edit the controller name as HomeController as shown in the figure below:


After adding HomeController, add an Index view into your project:



Edit the Index view and add the following code as given below. Also include these

files (jquery-ui.min.css, jquery-1.7.1.min.js, jquery-ui-1.8.20.min.js) in your project.

@model IEnumerable  @{      Layout = null;  }                  Index                                                                      
                @Html.ActionLink("Create New", "AddEditRecord", "Home", null, new { @id = "openDialog" })            
                                                                                        @foreach (var item in Model)         {                                                                                                                 }    
                @Html.DisplayNameFor(model => model.Name)                             @Html.DisplayNameFor(model => model.Age)                             @Html.DisplayNameFor(model => model.State)                             @Html.DisplayNameFor(model => model.Country)                        
                    @Html.DisplayFor(modelItem => item.Name)                                     @Html.DisplayFor(modelItem => item.Age)                                     @Html.DisplayFor(modelItem => item.State)                                     @Html.DisplayFor(modelItem => item.Country)                                     @Html.ActionLink("Edit", "AddEditRecord", new { id = item.StudentID }, new { @class = "editDialog" })|                     @Html.ActionLink("Details", "Details", new { id = item.StudentID }, new { @class = "viewDialog" }) |                     @Html.ActionLink("Delete", "DeleteRecord", new { id = item.StudentID }, new { @class = "confirmDialog" })                
           

 I have put all the code necessary to perform add, edit, delete and find records of the students in the Index view. In this project we do not have any Layout or Master page.

After including files in the project, your solution explorer might something look like this:



Add a partial view _StudentData in Views à Home folder.



Edit the _StudentData partial view as below:

@model Modal_CRUD_MVC.Models.Student  
@using (Ajax.BeginForm("AddEditRecord", "Home", new AjaxOptions{ HttpMethod = "POST", UpdateTargetId = "studentDialog" }))
{
    @Html.ValidationSummary(true)

   

        Student
        @if (ViewBag.IsUpdate == true)
        {
            @Html.HiddenFor(model => model.StudentID)
        }
       

            @Html.LabelFor(model => model.Name)
       

       

            @Html.EditorFor(model => model.Name)
            @Html.ValidationMessageFor(model => model.Name)
       

       

            @Html.LabelFor(model => model.Age)
       

       

            @Html.EditorFor(model => model.Age)
            @Html.ValidationMessageFor(model => model.Age)
       

       

            @Html.LabelFor(model => model.State)
       

       

            @Html.EditorFor(model => model.State)
            @Html.ValidationMessageFor(model => model.State)
       

       

            @Html.LabelFor(model => model.Country)
       

       

            @Html.EditorFor(model => model.Country)
            @Html.ValidationMessageFor(model => model.Country)
       

       


            @if (ViewBag.IsUpdate == true)
            {
               
            }
            else
            {
               
            }
           
       


   

}

 Also add StudentData view in the same folder Views à Home. This view will response by the controller to the browser if the JavaScript is turned off otherwise the above partial view will display in a modal dialog to the user.

@model Modal_CRUD_MVC.Models.Student  
@using (Html.BeginForm("AddEditRecord", "Home"))
{
    @Html.ValidationSummary(true)

   

        Student
        @if (ViewBag.IsUpdate == true)
        {
            @Html.HiddenFor(model => model.StudentID)
        }
       

            @Html.LabelFor(model => model.Name)
       

       

            @Html.EditorFor(model => model.Name)
            @Html.ValidationMessageFor(model => model.Name)
       

       

            @Html.LabelFor(model => model.Age)
       

       

            @Html.EditorFor(model => model.Age)
            @Html.ValidationMessageFor(model => model.Age)
       

       

            @Html.LabelFor(model => model.State)
       

       

            @Html.EditorFor(model => model.State)
            @Html.ValidationMessageFor(model => model.State)
       

       

            @Html.LabelFor(model => model.Country)
       

       

            @Html.EditorFor(model => model.Country)
            @Html.ValidationMessageFor(model => model.Country)
       

       


            @if (ViewBag.IsUpdate == true)
            {
               
            }
            else
            {
               
            }
            @Html.ActionLink("Back to List", "Index")
       
   
}

 Add a partial view _StudentDetails in Views à Home folder.

Edit the _StudentDetails partial view as below:

@model Modal_CRUD_MVC.Models.Student  


    Student

   

         @Html.DisplayNameFor(model => model.Name)
   

   

        @Html.DisplayFor(model => model.Name)
   


   

         @Html.DisplayNameFor(model => model.Age)
   

   

        @Html.DisplayFor(model => model.Age)
   


   

         @Html.DisplayNameFor(model => model.State)
   

   

        @Html.DisplayFor(model => model.State)
   


   

         @Html.DisplayNameFor(model => model.Country)
   

   

        @Html.DisplayFor(model => model.Country)
   

 

Add and Edit StudentDetails in Views => Home folder.

The purpose of adding StudentDetails view is same as I have explained above for StudentData view.

@model Modal_CRUD_MVC.Models.Student  

@{
    Layout = null;
}





   
    StudentDetails


   

        Student

       

             @Html.DisplayNameFor(model => model.Name)
       

       

            @Html.DisplayFor(model => model.Name)
       


       

             @Html.DisplayNameFor(model => model.Age)
       

       

            @Html.DisplayFor(model => model.Age)
       


       

             @Html.DisplayNameFor(model => model.State)
       

       

            @Html.DisplayFor(model => model.State)
       


       

             @Html.DisplayNameFor(model => model.Country)
       

       

            @Html.DisplayFor(model => model.Country)
       

   

   


        @Html.ActionLink("Edit", "Edit", new { id = Model.StudentID }, new { @class = "editDialog" }) |
        @Html.ActionLink("Back to List", "Index")
   



 I have finished Model and View portion. Now I am going to explain about the HomeController and its Action methods which are necessary in order to run our application.

Index action returns a View along with the list of students:

    public class HomeController : Controller  
    {
        StudentContext db = new StudentContext();

        public ActionResult Index()
        {
            return View(db.Students.ToList());
        }
    }

 Add the AddEditRecord action with the parameter (id) which can be either null or not null. If the id parameter is null it will return empty _StudentData or StudentData, partial view or View respectively otherwise it will find the record of student on the basis of id and passes the student model to the view in order to update the student details.

Here I am also creating a dynamic property ViewBag.IsUpdate to change the button type in view. If the IsUpdate is true it will set button text as Update and if IsUpdate is false then it will be set as Save.

 [HttpGet]  
        public ActionResult AddEditRecord(int? id)
        {
            if (Request.IsAjaxRequest())
            {
                if (id != null)
                {
                    ViewBag.IsUpdate = true;
                    Student student = db.Students.Where(m => m.StudentID == id).FirstOrDefault();
                    return PartialView("_StudentData", student);
                }
                ViewBag.IsUpdate = false;
                return PartialView("_StudentData");
            }
            else
            {
                if (id != null)
                {
                    ViewBag.IsUpdate = true;
                    Student student = db.Students.Where(m => m.StudentID == id).FirstOrDefault();
                    return PartialView("StudentData", student);


This post first appeared on Microsoft SharePoint, please read the originial post: here

Share the post

CRUD OPERATION USING MODAL DIALOG IN ASP.NET MVC

×

Subscribe to Microsoft Sharepoint

Get updates delivered right to your inbox!

Thank you for your subscription

×