Saturday, February 22, 2014

MVC With CURD Operations

File->New Project




Select Asp.net MVC 3 or MVC4 web application






Select the type of Template as Internet Application
View Engine: Razor

And Click on Ok Button.


Goto Views (Layout.cs.html)





And do the following changes in the code as below screen shot

    <li>@Html.ActionLink("Employee","Employee","Employee")</li>

In the above line

First parameter- Text

Second parameter-Method name or Action Name

Third parameter- Controller Name



Next Go to Model and create a class.










Give the name of the class as Employee.cs
Employees.cs

public class Employees
    {
        public int? Eno { get; set; }

        public string Ename { get; set; }

        public string Address { get; set; }


        public List<Employees> GetAllEmployees()
        {
            DataTable objTbl = new DataTable();
     List<Employees> employeeList = newSystem.Collections.Generic.List<Employees>();
            SqlConnection objCon = new SqlConnection();
            objCon.ConnectionString = "";
            SqlDataAdapter objAdp = new SqlDataAdapter("select * from employees", objCon);
            objAdp.Fill(objTbl);
            foreach (DataRow dr in objTbl.Rows)
            {
                employeeList.Add(new Employee()
                {
                    Eno = Convert.ToInt32(dr["ENO"]),
                    Ename = Convert.ToString(dr["Ename"]),
                    Address = Convert.ToString(dr["Address"])
                });
            }
            return employeeList;
        }
     

    }
Now the model is ready.

Next go to controller.

Add->Controller









And write the following code









Right click on the page




Select Add View






Now the view is created.


And run the application to test the summary Page.





To create:


Write the following lines of code in Employee Controller






Again write click on the controller









And click on Add Button


And run the application.



And click on Create New.



And again write the following lines of code in Employee Controller

[HttpPost]
        public ActionResult Create(Employees obj)
        {
            Employees objemp = new Employees();
            objemp.SaveEmployeeDetails(obj);
            return RedirectToAction("Employee");
        }







And write the following code in Employees.cs

And write the following code in Employees.cs
public void SaveEmployeeDetails(Employees obj)
        {
            SqlConnection objCon = new SqlConnection();
            objCon.ConnectionString = "";
            SqlCommand cmd = new SqlCommand();
            cmd.CommandText = "insert into Employees(Eno,Ename,Address) values(" + obj.Eno + ",'"+ obj.Ename + "','" + obj.Address + "')";
            objCon.Open();
            cmd.Connection = objCon;
            cmd.CommandType = CommandType.Text;
            cmd.ExecuteNonQuery();
            objCon.Close();

        }




Details:


Now go to Employee.cshtml (View Folder)




And modify the following lines of code.

  @Html.ActionLink("Details", "Details", new { id=item.Eno  })


Here Eno is the primary key of my table.





Go to Employee controller.cs

public ActionResult Details(int id)
        {
            Employees obj = Employees.GetEmployeesById(id);
            if (obj == null)
            {
                return HttpNotFound();
            }
            return View(obj);
        }

Go to Employees.cs and write the following code.


  public static Employees GetEmployeesById(int Eno)
        {
            DataTable objTbl = new DataTable();

            SqlConnection objCon = new SqlConnection();
            objCon.ConnectionString = "";

            SqlDataAdapter objAdp = new SqlDataAdapter("select * from employees where eno =" + Eno, objCon);

            objAdp.Fill(objTbl);

            Employees objEmployees = new Employees() { Eno = Convert.ToInt32(objTbl.Rows[0]["Eno"]), Ename = Convert.ToString(objTbl.Rows[0]["Ename"]), Address = Convert.ToString(objTbl.Rows[0]["Address"]) };

            return objEmployees;
        }



And right click on the Employee Controller and select Add View








Edit

Employee.cshtml


@Html.ActionLink("Details", "Details", new { id=item.Eno  }) 





Go to Employee Controller and write the following Code

[HttpGet]
        public ActionResult Edit(int id)
        {
            Employees obj = Employees.GetEmployeesById(id);
            if (obj == null)
            {
                return HttpNotFound();
            }
            return View(obj);
        }




Again Right Click on the Employee Controller and Add View







And click on Add Button.


And test the application.



And click on Edit Button.



Again go to Employee Controller.cs and write the following code to save the modified values.

[HttpPost]
        public ActionResult Edit(Employees obj)
        {
            Employees objemp = new Employees();
            objemp.UpdateEmployeeDetails(obj);
            return RedirectToAction("Employee");


        }






Employees.cs and write the following code


Now update the values and check the summary page.

Delete

Goto Employee.cshtml and modified  the following code .

           @Html.ActionLink("Edit", "Edit", new { id=item.Eno  }) |
            @Html.ActionLink("Details", "Details", new{ id=item.Eno  }) |
            @Html.ActionLink("Delete", "Delete", new { id=item.Eno })

Then go to EmployeeController.cs and write the following code in it.

public ActionResult Delete(int id)
        {
            Employees objemp = new Employees();
            objemp.DeleteEmployeeDetails(id);
            return RedirectToAction("Employee");
        }



Go To Employee.cs and write the following code.



Points to Remember

Here int id in the parameter should be same as
  @Html.ActionLink("Details", "Details", new { id=item.Eno  })

Both the names should be same

Kubernetes

Prerequisites We assume anyone who wants to understand Kubernetes should have an understating of how the Docker works, how the Docker images...