Monday, 4 June 2012

insert and delete in MVC using json

View page Code:
<%@ Page Language="C#" MasterPageFile="~/Views/Shared/Site.Master" Inherits="System.Web.Mvc.ViewPage<MvcApplication45.Models.EMP>" %>

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

<asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="server">
  <script language="javascript" type="text/javascript" src="../../Scripts/jquery-1.4.1.js"></script>
  <%using (Html.BeginForm())
    { %>
        <form id="f">
        <table align="center">
        <tr><td>EID</td><td><%=Html.TextBoxFor(Model=>Model.EID) %></td></tr>
        <tr><td>NAME</td><td><%=Html.TextBoxFor(Model=>Model.NAME) %></td></tr>
        <tr><td>&nbsp;</td><td><input type="button" id="btn" name="btn" value="Save" /></td></tr>
        <tr><td colspan="2">
        <table id="tb" width="100%"></table>
        <script language="javascript" type="text/javascript">
            $(document).ready
        (
        function () {
            function x(s) {
                $.getJSON('/Home/y', { a: s }, function (data) {
                    $('#tb').html('');
                    $('#tb').append("<tr><td>DELETE</td><td>EID</td><td>NAME</td></tr>")
                    for (var i = 0; i < data.length; i++)
                        $('#tb').append("<tr><td><a href='#' onclick='x(" + data[i]['EID'] + ")'>Delete</a></td><td>" + data[i]['EID'] + "</td><td>" + data[i]['NAME'] + "</td></tr>");

                });
            }
            function dis(data) {
                $('#tb').html('');
                $('#tb').append("<tr><td>DELETE</td><td>EID</td><td>NAME</td></tr>")
                for (var i = 0; i < data.length; i++)
                    $('#tb').append("<tr><td><a href='#' onclick='x(" + data[i]['EID'] + ")'>Delete</a></td><td>" + data[i]['EID'] + "</td><td>" + data[i]['NAME'] + "</td></tr>");
            }
            $('#btn').click(
        function () {
            $.getJSON('/Home/x', { a: $('#EID').val(), b: $('#NAME').val() }, function (data) {
                dis(data)
            })

        }
        )
        }
        )
        </script>
        </td></tr>
        </table>
        </form>
  <%} %>
</asp:Content>
Control page code:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using MvcApplication45.Models;

namespace MvcApplication45.Controllers
{
    [HandleError]
    public class HomeController : Controller
    {
        DataClasses1DataContext o = new DataClasses1DataContext();
        public ActionResult Index()
        {
            ViewData["Message"] = "Welcome to ASP.NET MVC!";

            return View();
        }

        public ActionResult About()
        {
            return View();
        }
        public JsonResult y(int a)
        {
            var m = (from z in o.EMPs where z.EID == a select z).First();
            o.EMPs.DeleteOnSubmit(m);
            o.SubmitChanges();
            return Json(o.EMPs.ToList(), JsonRequestBehavior.AllowGet);
        }
        public JsonResult x(int a, string b)
        {
            EMP p = new EMP();
            p.EID = a;
            p.NAME = b;
            o.EMPs.InsertOnSubmit(p);
            o.SubmitChanges();
            var m=(from y in o.EMPs select y).ToList();
            return Json(m, JsonRequestBehavior.AllowGet);
        }
    }
}

No comments:

Post a Comment