Thursday, 25 June 2015

basic crud

create database Employee
USE Employee
create table EMPDETAILS
(
EmployeeId  int  identity primary key not null,
Employeename varchar(100) not null,
EmployeeAge   int not null,
EmployeeCountry varchar(50) not null,
EmployeeSex   varchar(50) not null,
)

Select * from EMPDETAILS

insert into  EMPDETAILS values('sahir',19,'India','male')


==========

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Sample_application.Models;

namespace Sample_application.Controllers
{
    public class HomeController : Controller
    {
        public EmployeeEntities ec = new EmployeeEntities();

        public ActionResult Index()
        {
            return View(ec.EMPDETAILS.ToList());
        }

        [HttpGet]
        public ActionResult Edit(int id)
           
        {
            var employee = ec.EMPDETAILS.SingleOrDefault(p => p.EmployeeId == id);
            if(employee == null)
            {
                return HttpNotFound();

            }
            return View(employee);
        }


        [HttpPost]
        public ActionResult Edit(int id,EMPDETAIL empDet)
        {
            var employee = ec.EMPDETAILS.Single(p=>p.EmployeeId == id);

            employee.Employeename = empDet.Employeename;
            employee.EmployeeAge = empDet.EmployeeAge;
            employee.EmployeeCountry = empDet.EmployeeCountry;
            employee.EmployeeSex = empDet.EmployeeSex;
            ec.SaveChanges();
            return RedirectToAction("Index");
           
        }

        [HttpGet]
        public ActionResult Delete(int? id)
        {
            var employee = ec.EMPDETAILS.Find(id);

           if(employee==null)
           {
               return HttpNotFound();

           }
           return View(employee);
         
        }

        [HttpPost,ActionName("Delete")]
        public ActionResult DeleteConfirmed(int? id)
        {
            var employee = ec.EMPDETAILS.Find(id);
            ec.EMPDETAILS.Remove(employee);
            ec.SaveChanges();
            return RedirectToAction("Index");

        }
    }
}

=====

@model IEnumerable<Sample_application.Models.EMPDETAIL>

@{
    ViewBag.Title = "Index";
}

<h2>Index</h2>

<p>
    @Html.ActionLink("Create New", "Create")
</p>
<table>
    <tr>
        <th>
            @Html.DisplayNameFor(model => model.EmployeeId)
        </th>
        <th>
            @Html.DisplayNameFor(model => model.Employeename)
        </th>
        <th>
            @Html.DisplayNameFor(model => model.EmployeeAge)
        </th>
        <th>
            @Html.DisplayNameFor(model => model.EmployeeCountry)
        </th>
        <th>
            @Html.DisplayNameFor(model => model.EmployeeSex)
        </th>
        <th></th>
    </tr>

@foreach (var item in Model) {
    <tr>
        <td>
            @Html.DisplayFor(modelItem => item.EmployeeId)
        </td>
        <td>
            @Html.DisplayFor(modelItem => item.Employeename)
        </td>
        <td>
            @Html.DisplayFor(modelItem => item.EmployeeAge)
        </td>
        <td>
            @Html.DisplayFor(modelItem => item.EmployeeCountry)
        </td>
        <td>
            @Html.DisplayFor(modelItem => item.EmployeeSex)
        </td>
        <td>
            @Html.ActionLink("Edit", "Edit", new { id=item.EmployeeId  }) |
            @Html.ActionLink("Details", "Details", new { /* id=item.PrimaryKey */ }) |
            @Html.ActionLink("Delete", "Delete", new {  id=item.EmployeeId })
        </td>
    </tr>
}
</table>

No comments:

Post a Comment