Sunday, 12 January 2014

Learn Mvc3 (insert ,update ,delete using edmx with mvc)

http://www.pluralsight.com/training/Courses/TableOfContents/aspdotnet-mvc3-intro
http://www.w3schools.com/aspnet/mvc_intro.asp
http://malathyluxman.blogspot.in/2010/10/aspnet-mvc-viewcreate-edit-delete.html?showComment=1389597381543#c375097420805422145
http://www.c-sharpcorner.com/UploadFile/cd3310/createeditdelete-operation-on-a-table-in-Asp-Net-mvc-appli/





       First Create Edmx for connect with database
     second create controller
  ex:right click controller and add new controller  here i use employeeController

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using MvcApplication1.Models;
namespace MvcApplication1.Controllers
{
    public class EmployeeController : Controller
    {
        //
        // GET: /Employee/



       
        CompanyEntities _db = new CompanyEntities(); //object of entity class
      
        public ActionResult Index()
        {
            return View(_db.Employees.ToList());
        }

        //
        // GET: /Employee/Details/5

        public ActionResult Details(Employee empdata)
        {
            var v = _db.Employees.ToList();
            return View(v);
        }

        //
        // GET: /Employee/reate

        public ActionResult Create()
        {
            return View();
        }

        //
        // POST: /Employee/Create

        [HttpPost]
        public ActionResult Create(Employee empData)
        {
            try
            {
                // TODO: Add insert logic here



                if (!ModelState.IsValid)
                    return View();
                _db.AddToEmployees(empData);
                _db.SaveChanges();
                return RedirectToAction("Index");
             
            }
            catch
            {
                return View();
            }
        }
       
        //
        // GET: /Employee/Edit/5

        public ActionResult Edit(int id)
        {
            var empToEdit = (from emp in _db.Employees where emp.ID.Equals(id) select emp).First();
            return View();
        }

        //
        // POST: /Employee/Edit/5

        [HttpPost]
        public ActionResult Edit(Employee empToEdit)
        {
            try
            {
                // TODO: Add update logic here

                var originalEmp = (from emp in _db.Employees where emp.ID.Equals(empToEdit.ID) select emp).First();
                if (!ModelState.IsValid)
                    return View(originalEmp);
                _db.ApplyPropertyChanges(originalEmp.EntityKey.EntitySetName, empToEdit);
                _db.SaveChanges();
                return RedirectToAction("Index");
              
           
            }
            catch
            {
                return View();
            }
        }

        //
        // GET: /Employee/Delete/5

        public ActionResult Delete(int id)
        {
            try
            {
                // TODO: Add delete logic here

                var empToDelete = (from emp in _db.Employees where emp.ID.Equals(id) select emp).First();
                return View(empToDelete);


            }
            catch
            {
                return View();
            }
        }

        //
        // POST: /Employee/Delete/5

        [HttpPost]
        public ActionResult Delete(int id,string s)
        {
            try
          {
             // var vv = _db.Employees.Where(a => a.ID == id).ToList();
          var originalEmp = (from emp in _db.Employees where emp.ID.Equals(id) select emp).First();
           _db.DeleteObject(originalEmp);
          _db.SaveChanges();
           return RedirectToAction("Index");
          }
          catch (Exception ex)
            {
                 return View();
            }
        
        }
    }
}


then create Employee floder in view
in employee create  index,create,edit,detail,delete.aspx (ex:right click employee and use first option view)
in view second dropdown use namespace.edmx.table name select

third box select create-if create,edit-if edit,list if index




ex:Index.aspx
<%@ Page Title="" Language="C#" MasterPageFile="~/Views/Shared/Site.Master" Inherits="System.Web.Mvc.ViewPage<IEnumerable<MvcApplication1.Models.Employee>>" %>

<asp:Content ID="Content1" ContentPlaceHolderID="TitleContent" runat="server">
    Index
</asp:Content>

<asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="server">

    <h2>Index</h2>

    <table>
        <tr>
            <th></th>
            <th>
                FirstName
            </th>
            <th>
                LastName
            </th>
            <th>
                ID
            </th>
            <th>
                Designation
            </th>
        </tr>

    <% foreach (var item in Model) { %>
   
        <tr>
            <td>
                <%: Html.ActionLink("Edit", "Edit", new { id=item.ID }) %> |
                <%: Html.ActionLink("Details", "Details", new { id=item.ID })%> |
                <%: Html.ActionLink("Delete", "Delete", new { id=item.ID })%>
            </td>
            <td>
                <%: item.FirstName %>
            </td>
            <td>
                <%: item.LastName %>
            </td>
            <td>
                <%: item.ID %>
            </td>
            <td>
                <%: item.Designation %>
            </td>
        </tr>
   
    <% } %>

    </table>

    <p>
        <%: Html.ActionLink("Create New", "Create") %>
    </p>

</asp:Content>

//create.aspx
<%@ Page Title="" Language="C#" MasterPageFile="~/Views/Shared/Site.Master" Inherits="System.Web.Mvc.ViewPage<MvcApplication1.Models.Employee>" %>



 

No comments:

Post a Comment