Thursday, 4 August 2016

Multiple delete,all controls &CURD oration in NG GRIDE, MVC ,WEB API, & ANGULARJS

web api code :

using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web.Http;
using WebApplication36.Models;

namespace WebApplication36.Controllers
{
    
    public class ValuesController : ApiController
    {
        [HttpGet]
        public List<COUNTRY> Getc()
        {
            using (Database1Entities obj = new Database1Entities())
            {
                return obj.COUNTRies.ToList();
            }
        }
        [HttpGet]
        public List<STATE> Gets(int CID)
        {
            using (Database1Entities obj = new Database1Entities())
            {
                return obj.STATEs.Where(m=>m.CID==CID).ToList();
            }
        }
        [HttpGet]
        public List<EMP> Gete(int i,int j)
        {
            using (Database1Entities obj = new Database1Entities())
            {
                return obj.EMPs.ToList();
            }
        }
        [HttpGet]
        public EMP Get(int EID,int i, int j)
        {
            using (Database1Entities obj = new Database1Entities())
            {
                return obj.EMPs.SingleOrDefault(m=>m.EID==EID);
            }
        }
        [HttpPost]
        public string Post(EMP emp)
        {
            using (Database1Entities obj = new Database1Entities())
            {
               obj.EMPs.Add(emp);
               obj.SaveChanges();
               return "Data Saved.";
            }
        }
        [HttpPut]
        public string Put(EMP emp)
        {
            using (Database1Entities obj = new Database1Entities())
            {
                EMP emp1 = obj.EMPs.SingleOrDefault(m => m.EID == emp.EID);
                emp1.NAME = emp.NAME;
                emp1.ADDRESS = emp.ADDRESS;
                emp1.PASSWORD = emp.PASSWORD;
                emp1.DOB = emp.DOB;
                emp1.GENDER = emp.GENDER;
                emp1.ISMARRIED = emp.ISMARRIED;
                emp1.CID = emp.CID;
                emp1.SID = emp.SID;
                obj.SaveChanges();
                return "Data Updated.";
            }
        }
        [HttpGet]
        public string Delete(int EID)
        {
            using (Database1Entities obj = new Database1Entities())
            {
                EMP emp1 = obj.EMPs.SingleOrDefault(m => m.EID == EID);
                obj.EMPs.Remove(emp1);
                obj.SaveChanges();
                return "Data Deleted.";
            }
        }
        [HttpDelete]
        public string Mdelete([FromUri]List<int> lst)
        {
            using (Database1Entities obj = new Database1Entities())
            {
                obj.EMPs.RemoveRange(obj.EMPs.Where(m=>lst.Contains(m.EID)));
                obj.SaveChanges();
                return "Data Deleted.";
            }
        }
    }
}

View Code :



@{
    ViewBag.Title = "Index";
}

<h2>Jay Jagannath</h2>
<style type="text/css">
    .gridStyle {
        border: 1px solid rgb(212,212,212);
        width: 900px;
        height: 300px;
    }
</style>
<link href="~/Content/bootstrap.css" rel="stylesheet" />
<link href="~/Content/ng-grid.css" rel="stylesheet" />
<link href="~/Content/bootstrap-datepicker.css" rel="stylesheet" />
<script src="~/Scripts/jquery-1.9.1.js"></script>
<script src="~/Scripts/angular.js"></script>
<script src="~/Scripts/ng-grid.js"></script>
<script src="~/Scripts/bootstrap-datepicker.js"></script>
<script src="~/Scripts/Service.js"></script>
<div class="container" ng-app="app" ng-controller="ctr">
    <form class="form-horizontal">
        <div class="form-group">
            <label class="col-lg-4 control-label">EID</label>
            <div class="col-lg-4">
                <input type="text" class="form-control" ng-model="EMP.EID" id="tb"/>
            </div>
        </div>
        <div class="form-group">
            <label class="col-lg-4 control-label">NAME</label>
            <div class="col-lg-4">
                <input type="text" class="form-control" ng-model="EMP.NAME" />
            </div>
        </div>
        <div class="form-group">
            <label class="col-lg-4 control-label">ADDRESS</label>
            <div class="col-lg-4">
                <textarea  class="form-control" ng-model="EMP.ADDRESS" ></textarea>
            </div>
        </div>
        <div class="form-group">
            <label class="col-lg-4 control-label">PASSWORD</label>
            <div class="col-lg-4">
                <input type="password" class="form-control" ng-model="EMP.PASSWORD" />
            </div>
        </div>
        <div class="form-group">
            <label class="col-lg-4 control-label">DOB</label>
            <div class="col-lg-4">
                <input type="text" class="form-control" ng-model="EMP.DOB" id="dob" />
            </div>
        </div>
        <div class="form-group">
            <label class="col-lg-4 control-label">GENDER</label>
            <div class="col-lg-4">
                <input type="radio" value="Male" ng-model="EMP.GENDER" />Male
                <input type="radio" value="Female" ng-model="EMP.GENDER" />Female
            </div>
        </div>
        <div class="form-group">
            <label class="col-lg-4 control-label">ARE YOU MARRIED?</label>
            <div class="col-lg-4">
                <input type="checkbox" ng-model="EMP.ISMARRIED" />
            </div>
        </div>
        <div class="form-group">
            <label class="col-lg-4 control-label">COUNTRY</label>
            <div class="col-lg-4">
            <select class="form-control" ng-model="EMP.CID" ng-options="c.CID as c.CNAME for c in listc" ng-change="fillddl()" >
                <option value="">Select</option>
            </select>
            </div>
        </div>
        <div class="form-group">
            <label class="col-lg-4 control-label">STATE</label>
            <div class="col-lg-4">
                <select class="form-control" ng-model="EMP.SID" ng-options="c.SID as c.SNAME for c in lists">
                    <option value="">{{text}}</option>
                </select>
            </div>
        </div>
        <div class="form-group">
            <label class="col-lg-4 control-label"></label>
            <div class="col-lg-4">
                <input type="button" value="Delete All" style="width:80px" class="btn btn-primary" ng-click="da()" />
                <input type="button" value="Save" style="width:80px" class="btn btn-primary" ng-click="save()" />
                <input type="button" value="Update" style="width:80px" class="btn btn-primary" ng-click="updates()" />
                <input type="button" value="Reset" style="width:80px" class="btn btn-primary" ng-click="reset()" />
            </div>
        </div>
        <div class="gridStyle" ng-grid="gridOptions">

        </div>
    </form>
</div>

Angular code :(//Service.js file code)
/// <reference path="jquery-1.9.1.js" />
/// <reference path="angular.js" />
/// <reference path="ng-grid.js" />
/// <reference path="bootstrap-datepicker.js" />
var k;
$(function () {
    $('#dob').datepicker({ format: 'yyyy-mm-dd' }).on("change", function () {
        k = $(this).val();
    });
});
angular.module("app", ['ngGrid'])
.controller("ctr", function ($scope, EmpService) {
    var p = '';
    $scope.listc = null;
    $scope.lists = null;
    $scope.n = [];
    $scope.save = function ()
    {
        $scope.EMP.DOB = k;
        EmpService.Save($scope.EMP).then(function (d) {
            alert(d.data);
            clr();
            fill();
        });
    }
    $scope.updates = function ()
    {
        if (k != undefined)
            $scope.EMP.DOB = k;
        EmpService.Update($scope.EMP).then(function (d) {
            alert(d.data);
            clr();
            fill();
        });
    }
    $scope.Del = function (s) {
        if (confirm('Do you want to delete it ?')) {
            EmpService.Delete(s.EID).then(function (d) {
                alert(d.data);
                fill();
            });
        }
    }
    $scope.da = function ()
    {
        if (confirm('Do you want to delete it ?'))
        {
            angular.forEach($scope.n, function (i, j) {
                p = p + 'lst=' + i.EID + '&';
            });
            p = p.substring(0, p.lastIndexOf('&'));
            EmpService.Mdelete(p).then(function (d) {
                alert(d.data);
                fill();
            });
        }
    }
    $scope.reset = function ()
    {
        clr();
        $scope.lists = null;
        $scope.text = null;
    }
    function clr()
    {
        $scope.EMP = new empVM();
        $('#tb').focus();
    } clr();
    EmpService.GetCountries().then(function (d) {
        $scope.listc = d.data;
    });
    $scope.fillddl = function () {
        fd($scope.EMP.CID);
    }
    function fd(s)
    {
        EmpService.GetStates(s).then(function (d) {
            $scope.text = "Select";
            $scope.lists = d.data;
        });
    }
    function fill() {
        $scope.filterOptions = {
            filterText: "",
            useExternalFilter: false
        };
        $scope.totalServerItems = 0;
        $scope.pagingOptions = {
            pageSizes: ['5', '10', '20'],
            pageSize: '5',
            currentPage: 1
        };
        $scope.setPagingData = function (data, page, pageSize) {
            var pagedData = data.slice((page - 1) * pageSize, page * pageSize);
            $scope.DataSource = pagedData;
            $scope.totalServerItems = data.length;
            if (!$scope.$$phase) {
                $scope.$apply();
            }
        };
        $scope.getPagedDataAsync = function (pageSize, page, searchText) {
            setTimeout(function () {
                var data;
                if (searchText) {
                    var ft = searchText.toLowerCase();
                    EmpService.GetEmps().then(function (d) {
                        data = d.data.filter(function (item) {
                            return JSON.stringify(item).toLowerCase().indexOf(ft) != -1;
                        });
                        $scope.setPagingData(data, page, pageSize);
                    });
                  
                } else {
                    EmpService.GetEmps().then(function (d) {
                        $scope.setPagingData(d.data, page, pageSize);
                    });
                }
            }, 100);
        };

        $scope.getPagedDataAsync($scope.pagingOptions.pageSize, $scope.pagingOptions.currentPage);

        $scope.$watch('pagingOptions', function (newVal, oldVal) {
            if (newVal !== oldVal && newVal.currentPage !== oldVal.currentPage) {
                $scope.getPagedDataAsync($scope.pagingOptions.pageSize, $scope.pagingOptions.currentPage, $scope.filterOptions.filterText);
            }
        }, true);
        $scope.$watch('filterOptions', function (newVal, oldVal) {
            if (newVal !== oldVal) {
                $scope.getPagedDataAsync($scope.pagingOptions.pageSize, $scope.pagingOptions.currentPage, $scope.filterOptions.filterText);
            }
        }, true);


        $scope.gridOptions = {
            data: 'DataSource',
            multiSelect: true,
            showSelectionCheckbox: true,
            enableCellEdit: false,
            enableRowSelection: true,
            enableColumnResize: false,
            enableCellSelection: true,
            enablePaging: true,
            showFooter: true,
            selectedItems: $scope.n,
            totalServerItems: 'totalServerItems',
            pagingOptions: $scope.pagingOptions,
            filterOptions: $scope.filterOptions,
            showFilter: true,
            columnDefs:
             [
      { field: 'EID', displayName: 'Eid', width: '250px', resizable: true },
      { field: 'NAME', displayName: 'Name', width: '250px' },
       { field: "DOB", displayName: "DOB", width: "200px", cellTemplate: "<span>{{row.entity.DOB | date:'dd-MMM-yyyy'}}</span>" },

   {
       displayName: 'Actions', cellTemplate:
        '<div class="grid-action-cell">' +
        '<a ng-click="Edit(row.entity);" >Edit</a>' + ' | ' +
        '<a ng-click="Del(row.entity);" >Delete</a>' +
        '</div>'
   }
             ],
        };


    } fill();

    $scope.Edit = function (s)
    {
        EmpService.GetEmp(s.EID).then(function (d) {
            fd(d.data.CID);
            $scope.EMP = d.data;
            $scope.EMP.DOB = $scope.EMP.DOB.substring(0, 10);
        });
    }
   
   
})
.factory("EmpService", function ($http) {
    var fac = {};
    fac.GetCountries = function () {
        return $http.get("http://localhost:50611/api/Values");
    }
    fac.GetStates = function (CID)
    {
        return $http.get("http://localhost:50611/api/Values/Gets?CID="+CID);
    }
    fac.GetEmps = function ()
    {
        return $http.get("http://localhost:50611/api/Values/Gete?i=" +1+"&j="+2);
    }
    fac.GetEmp = function (EID) {
        return $http.get("http://localhost:50611/api/Values/Get?EID="+EID+"&i=" + 1 + "&j=" + 2);
    }
    fac.Save = function (EMP)
    {
        return $http.post("http://localhost:50611/api/Values/Post", EMP);
    }
    fac.Update = function (EMP) {
        
        return $http.put("http://localhost:50611/api/Values/Put", EMP);
      
    }
    fac.Delete = function (EID) {
        return $http.get("http://localhost:50611/api/Values/Delete?EID=" +EID);
    }
    fac.Mdelete = function (lst) {
        return $http.delete("http://localhost:50611/api/Values/Mdelete?" + lst);
    }
    return fac;
});
function empVM() {
    return {
        EID:null,
        NAME: null,
        ADDRESS: null,
        PASSWORD: null,
        DOB: null,
        GENDER: null,
        ISMARRIED: true,
        CID: null,
        SID: null
    }
}

Tuesday, 2 August 2016

dynamic page in MVC


@model Nop.Admin.Models.QuestionnaireManagement.QuestionnaireManagementVM

@using (Html.BeginForm())
{
    @Html.AntiForgeryToken()

    <div class="formDetails" style="border: none; padding:0; margin:0">
        @Html.ValidationSummary(true, "", new { @class = "text-danger" })

        <div class="col-md-12 col-sm-12 col-xs-12">
            <div class="form-group">
                @Html.LabelFor(model => model.Questions, htmlAttributes: new { @class = "" })<span class="mandatory"><b>*</b></span>
                @Html.HiddenFor(m => m.Id)
                @Html.HiddenFor(m => m.Type)
                @Html.HiddenFor(m => m.CreatedBy)
                @Html.HiddenFor(m => m.CreatedDate)
                @Html.HiddenFor(m => m.UpdatedBy)
                @Html.HiddenFor(m => m.UpdatedDate)
                @Html.HiddenFor(m => m.Mark)
                @Html.TextAreaFor(model => model.Questions, new { @class = "form-control" })
                @Html.ValidationMessageFor(model => model.Questions, "", new { @class = "text-danger", @id = "QuestionsError" })

            </div>
        </div>
        <div class="col-md-12 col-sm-12 col-xs-12">
            <div class="form-group">
                @Html.LabelFor(model => model.ControlTypeId, htmlAttributes: new { @class = "" })<span class="mandatory"><b>*</b></span>
                @Html.DropDownListFor(model => model.ControlTypeId, Model.ControlType, new { @class = "form-control" })
                @Html.ValidationMessageFor(model => model.ControlTypeId, "", new { @class = "text-danger", @id = "ct" })

            </div>
        </div>

        <div class="col-md-12 col-sm-12 col-xs-12">
            <div class="form-group">
                @Html.LabelFor(model => model.StatusId, htmlAttributes: new { @class = "" })<span class="mandatory"><b>*</b></span>
                @Html.DropDownListFor(model => model.StatusId, Model.Status, new { @class = "form-control" })
                @Html.ValidationMessageFor(model => model.StatusId, "", new { @class = "text-danger", @id = "st" })

            </div>
        </div>
        <div class="col-md-12 col-sm-12 col-xs-12" id="ValueDiv">
            <div class="form-group">
                <div class="col-md-7 zero-padding-left">
                    <label>Value</label>
                </div>

                <div class="col-md-3 ">
                    <a href="#" id="btnPlus"><img class="img-responsive" src="/Administration/Content/images/plus-icon.png"></a>
                </div>
                <div class="col-md-12 zero-padding-left c">

                    <div class="col-md-7 zero-padding-left">
                        <input type="text" class="form-control value" />
                    </div>
                    <div class="col-md-3 btn-css">
                        <a href="#" name="del"><img class="img-responsive" src="/Administration/Content/images/minus-icon.png"></a>
                    </div>
                </div>
            </div>
        </div>

        <div class="clearfix"></div>
        <div class="options k-button-add marginTopbtn">
            <input type="button" value="Save" class="k-button  btn-default padd-left-right" id="btnSubmit" />
            <input type="button" class="k-button k-button-cancel disabled padd-left-right" value="Cancel" id="btnCancel">
        </div>
    </div>
}

<style>
    .btn-css img {
        padding-top: 10px;
        padding-bottom: 10px;
        padding-left: 8px;
    }
</style>
<script type="text/javascript">
    $(function () {
        var row;
        $('#ValueDiv').hide();
        if ($('#ControlTypeId').val() == 1) {
            $('#ValueDiv').show();
        }
        else
            $('#ValueDiv').hide();
        $('#ControlTypeId').change(function () {
            if ($('#ControlTypeId').val() == 1) {
                $('#ValueDiv').show();
            }
            else
                $('#ValueDiv').hide();
        });
        row = $('.c').clone();
        $('#btnPlus').click(function () {

            var crow = row.clone();
            $('#ValueDiv').append(crow);
        });
        $('body').on('click', "a[name='del']", function () {
            var row = $(this).parents('.c');
            if ($('.c').length > 1)
                row.remove();
        });
        if(@Model.Mark==2 && @Model.ControlTypeId==1)
        {
            var data=@Html.Raw(Json.Encode(@Model.Values));
            $('#ValueDiv').find('.c').remove();
            for(var i=0;i<data.length;i++)
            {
                $('#ValueDiv').last().append("<div class='col-md-12 zero-padding-left c'><div class='col-md-7 zero-padding-left'><input type='text' value='"+data[i]+"' class='form-control value'/></div><div class='col-md-3 btn-css'><a href='#' name='del'><img class='img-responsive' src='/Administration/Content/images/minus-icon.png'></a></div></div>");
            }
        }
        $('#btnSubmit').click(function () {
            $('#QuestionsError').text('');
            $('#ct').text('');
            $('#st').text('');
            var check = 0;
            if ($('#Questions').val() == "") {
                $('#QuestionsError').text('Questions should not be blank.');
                $('#Questions').focus();
                check = 1;
            }
            if ($('#ControlTypeId').val() == 0) {

                $('#ct').text('Please select a control type.');

                check = 1;

            }
            if ($('#StatusId').val() == 0) {
                $('#st').text('Please select a active.');

                check = 1;
            }
            if (check == 1)
                return false;
            var marr = [];
            $.each($('.c'), function (i, j) {
                marr.push($(j).find('.value').val());
            });
            var questionnaireManagementVM = {
                Id: $('#Id').val(),
                Type: $('#Type').val(),
                CreatedBy: $('#CreatedBy').val(),
                CreatedDate: $('#CreatedDate').val(),
                UpdatedBy: $('#UpdatedBy').val(),
                UpdatedDate: $('#UpdatedDate').val(),
                Mark: $('#Mark').val(),
                Questions: $('#Questions').val(),
                ControlTypeId: $('#ControlTypeId').val(),
                StatusId: $('#StatusId').val(),
                Values: marr
            }
            $.ajax({
                cache: false,
                type: "Post",
                url: "/QuestionnaireManagement/AddQuestion",
                data: { questionnaireManagementVM: questionnaireManagementVM },
                success: function (data) {
                    location.reload(true);
                }
            });
        });

        $('#btnCancel').click(function () {
            var window = $('#FillAddQuestion');
            window.data('kendoWindow').close();
        })
    });
</script>

Sunday, 31 July 2016

angular js File Upload

Controller

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace WebApplication1.Controllers
{
    public class HomeController : Controller
    {
        // GET: Home
        public ActionResult Index()
        {
            return View();
        }
        [HttpPost]
        public JsonResult Post()
        {
            var file = Request.Files[0];
            file.SaveAs(Server.MapPath("~/Image/" + Path.GetFileName(file.FileName)));
            return Json(Request.Form["EID"] + " " + Request.Form["NAME"], JsonRequestBehavior.AllowGet);
        }
    }
}
View Code


@{
    ViewBag.Title = "Index";
}

<h2>Jay Jagannath...</h2>
<link href="~/Content/bootstrap.css" rel="stylesheet" />
<script src="~/Scripts/jquery-1.10.2.js"></script>
<script src="~/Scripts/angular.js"></script>
<div class="container" ng-app="app" ng-controller="ctr">
    <form class="form-horizontal" >
        <div class="form-group">
            <label class="col-lg-4 control-label">Eid</label>
            <div class="col-lg-4">
                <input type="text" class="form-control" ng-model="EID" />
            </div>
        </div>
        <div class="form-group">
            <label class="col-lg-4 control-label">Name</label>
            <div class="col-lg-4">
                <input type="text" class="form-control" ng-model="NAME" />
            </div>
        </div>
        <div class="form-group">
            <label class="col-lg-4 control-label">File Upload</label>
            <div class="col-lg-4">
                <input type="file" class="form-control" id="fu"  />
            </div>
        </div>
        <div class="form-group">
            <label class="col-lg-4 control-label"></label>
            <div class="col-lg-4">
                <input type="button" class="btn btn-primary" value="Save" ng-click="save()" />
            </div>
        </div>
    </form>
</div>
<script type="text/javascript">
    angular.module("app", []).controller("ctr", function ($scope, FileUploadService) {
        $scope.save = function () {
            var formData = new FormData();
            var file = document.getElementById("fu").files[0];
            formData.append("fu", file);
            formData.append("EID", $scope.EID);
            formData.append("NAME", $scope.NAME);
            FileUploadService.savefile(formData).then(function (d) {
                alert(d.data);
            });
        }

    }).factory("FileUploadService", function ($http) {

        var fac = {};
        fac.savefile = function (fd)
        {
            var config = {
                headers: {
                    'Content-Type': 'multipart/form-data;charset=utf-8;'
                }
            }
            return $http.post('/Home/Post', fd, config);
        }
        return fac;
    });
</script>

Saturday, 30 July 2016

cascading dropdown in angular js,MVC & web api


WEB API CODE :

using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web.Http;
using WebApplication33.Models;

namespace WebApplication33.Controllers
{
   
    public class ValuesController : ApiController
    {
        public List<COUNTRY> Getc()
        {
            using (Database1Entities obj = new Database1Entities())
            {
                return obj.COUNTRies.ToList();
            }
        }
        public List<STATE> Gets(int CID)
        {
            using (Database1Entities obj = new Database1Entities())
            {
                return obj.STATEs.Where(m => m.CID==CID).ToList();
            }
        }
        
    }
}

VIEW CODE :


@{
    ViewBag.Title = "Index";
}
<link href="~/Content/bootstrap.css" rel="stylesheet" />
<script src="~/Scripts/jquery-1.10.2.js"></script>
<script src="~/Scripts/angular.js"></script>
<h2>jay jagannath</h2>
<div class="container" ng-app="app" ng-controller="ctr">
    <form class="form-horizontal">
        <div class="form-group">
            <label class="col-lg-4 control-label">COUNTRY</label>
            <div class="col-lg-4">
                <select class="form-control" ng-options="c.CID as c.CNAME for c in listc" ng-model="CID" ng-change="fillddl()">
                    <option value="">Select</option>
                </select>
            </div>
        </div>
        <div class="form-group">
            <label class="col-lg-4 control-label">STATE</label>
            <div class="col-lg-4">
                <select class="form-control" ng-options="c.SID as c.SNAME for c in lists" ng-model="SID" >
                    <option value="">{{display}}</option>
                </select>
            </div>
        </div>
        <div class="form-group">
            <label class="col-lg-4 control-label"></label>
            <div class="col-lg-4">
                <input type="button" value="Result" ng-click="save()" class="btn btn-primary" />
            </div>
        </div>
        <div class="form-group">
            <label class="col-lg-4 control-label"></label>
            <div class="col-lg-4">
                {{result}}
            </div>
        </div>
    </form>
</div>
<script type="text/javascript">
    angular.module("app", [])
        .controller("ctr", function ($scope, EmpService) {
            $scope.listc = null;
            $scope.lists = null;
            $scope.CID = null;
            $scope.SID = null;
            EmpService.GetCountry().then(function (d) {
                $scope.listc = d.data;
            });
            $scope.fillddl = function ()
            {
                EmpService.GetState($scope.CID).then(function (d) {
                    $scope.display = "Select";
                    $scope.lists = d.data;
                });
            }
            $scope.save = function ()
            {
               
                $scope.result = "Selected country Id is :" + $scope.CID +" Selected state id is :" + $scope.SID;
            }
        })
        .factory("EmpService", function ($http) {
            var fac = {};
            fac.GetCountry = function ()
            {
                return $http.get('http://localhost:53550/api/Values');
            };
            fac.GetState = function (CID) {
                return $http.get('http://localhost:53550/api/Values/Gets?CID=' + CID);
            }
            return fac;
    });
</script>

Multiple delete,date picker &CURD oration in NG GRIDE, MVC ,WEB API, & ANGULARJS


Web Api Code :

using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web.Http;
using WebApplication32.Models;

namespace WebApplication32.Controllers
{
 
    public class ValuesController : ApiController
    {
        public List<string> Getc()
        {
            using (Database1Entities obj = new Database1Entities())
            {
                return obj.COUNTRies.Select(m=>m.CNAME).ToList();
            }
        }
        public List<string> Gets(string CNAME)
        {
            using (Database1Entities obj = new Database1Entities())
            {
                int cid = obj.COUNTRies.SingleOrDefault(m => m.CNAME == CNAME).CID;
                return obj.STATEs.Where(n=>n.CID==cid).Select(m => m.SNAME).ToList();
            }
        }
        public List<EMP> Gete(int i)
        {
            using (Database1Entities obj = new Database1Entities())
            {
                return obj.EMPs.ToList();
            }
        }
        public EMP Get(int EID, int j)
        {
            using (Database1Entities obj = new Database1Entities())
            {
                return obj.EMPs.SingleOrDefault(m => m.EID == EID);
            }
        }
        [HttpPost]
        public string Post(EMP emp)
        {
            using (Database1Entities obj = new Database1Entities())
            {
                obj.EMPs.Add(emp);
                obj.SaveChanges();
                return "Data Saved.";
            }
        }
        [HttpPut]
        public string Put(EMP emp)
        {
            using (Database1Entities obj = new Database1Entities())
            {
                EMP emp1 = obj.EMPs.SingleOrDefault(n => n.EID == emp.EID);
                emp1.NAME = emp.NAME;
                emp1.ADDRESS = emp.ADDRESS;
                emp1.DOB = emp.DOB;
                emp1.GENDER = emp.GENDER;
                emp1.ISMARRIED = emp.ISMARRIED;
                emp1.CNAME = emp.CNAME;
                emp1.SNAME = emp.SNAME;
                obj.SaveChanges();
                return "Data Updated.";
            }
        }
        [HttpPatch]
        public string Del(int EID)
        {
            using (Database1Entities obj = new Database1Entities())
            {
                EMP emp1 = obj.EMPs.SingleOrDefault(n => n.EID == EID);
                obj.EMPs.Remove(emp1);
                obj.SaveChanges();
                return "Data Deleted.";
            }
        }
        [HttpDelete]
        public string Mdelete([FromUri]List<int> lst)
        {
            using (Database1Entities obj = new Database1Entities())
            {
             
                obj.EMPs.RemoveRange(obj.EMPs.Where(n=>lst.Contains(n.EID)));
                obj.SaveChanges();
                return "Data Deleted.";
            }
        }
    }
}

Veiw Code :

@{
    ViewBag.Title = "Index";
}
<link href="~/Content/ng-grid.css" rel="stylesheet" />
<link href="~/Content/bootstrap.css" rel="stylesheet" />
<link href="~/Content/bootstrap-datepicker.css" rel="stylesheet" />
<script src="~/Scripts/jquery-1.10.2.js"></script>
<script src="~/Scripts/angular.js"></script>
<script src="~/Scripts/bootstrap-datepicker.js"></script>
<script src="~/Scripts/ng-grid.js"></script>

<h2>Jay Jagannath...</h2><br /><br />
<div class="container" ng-app="app" ng-controller="ctr">
    <form class="form-horizontal">
        <div class="form-group">
            <label class="col-lg-4 control-label">EID</label>
            <div class="col-lg-4">
                <input type="text" class="form-control" ng-model="EMP.EID" id="tb" />
            </div>
        </div>
        <div class="form-group">
            <label class="col-lg-4 control-label">NAME</label>
            <div class="col-lg-4">
                <input type="text" class="form-control" ng-model="EMP.NAME" />
            </div>
        </div>
        <div class="form-group">
            <label class="col-lg-4 control-label">ADDRESS</label>
            <div class="col-lg-4">
                <textarea class="form-control" ng-model="EMP.ADDRESS"></textarea>
            </div>
        </div>
        <div class="form-group">
            <label class="col-lg-4 control-label">DOB</label>
            <div class="col-lg-4">
                <input type="text" class="form-control c" ng-model="EMP.DOB" />
            </div>
        </div>
        <div class="form-group">
            <label class="col-lg-4 control-label">GENDER</label>
            <div class="col-lg-4">
                <input type="radio" value="Male" ng-model="EMP.GENDER" />Male
                <input type="radio" value="Female" ng-model="EMP.GENDER" />Female
            </div>
        </div>
        <div class="form-group">
            <label class="col-lg-4 control-label">ARE YOU MARRIED ?</label>
            <div class="col-lg-4">
                <input type="checkbox" ng-model="EMP.ISMARRIED" />
            </div>
        </div>
        <div class="form-group">
            <label class="col-lg-4 control-label">COUNTRY</label>
            <div class="col-lg-4">
                <select ng-options="c for  c in listc" ng-change="fillddl()" class="form-control" ng-model="EMP.CNAME">
                   
                </select>
            </div>
        </div>
        <div class="form-group">
            <label class="col-lg-4 control-label">STATE</label>
            <div class="col-lg-4">
                <select ng-options="c for  c in  lists" class="form-control" ng-model="EMP.SNAME">
                    
                </select>
            </div>
        </div>
        <div class="form-group">
            <label class="col-lg-4 control-label"></label>
            <div class="col-lg-4">
                <input type="button" class="btn btn-primary" value="Delete All" ng-click="delall()" style="width:80px" />
                <input type="button" class="btn btn-primary" value="Save" ng-click="save()" style="width:80px" />
                <input type="button" class="btn btn-primary" value="Update" ng-click="update()" style="width:80px" />
                <input type="button" class="btn btn-primary" value="Reset" ng-click="reset()" style="width:80px" />
            </div>
        </div>
        <div class="gridStyle" ng-grid="gridOptions">

        </div>
    </form>
</div>
<style type="text/css">
    .gridStyle {
        border: 1px solid rgb(212,212,212);
        width: 900px;
        height: 300px;
    }
</style>
<script type="text/javascript">
    var k;
    $('.c').datepicker({ format: 'yyyy-mm-dd' }).on("change", function () {
       k=$(this).val();
    });
    angular.module("app", ['ngGrid']).controller("ctr", function ($scope, $http) {
        var m;
        $scope.n = [];
        function clr() {
            $scope.EMP = new emp();
            $('#tb').focus();
        } clr();
        $scope.save = function () {
            $scope.EMP.DOB = k;
            alert($scope.EMP.DOB);
            m = $http({
                url: 'http://localhost:59600/api/Values/Post',
                method: 'Post',
                data: $scope.EMP,
            });
            m.then(function (d) {
                alert(d.data);
                clr();
                fill();
            });
        }
        function filldl() {
            m = $http({
                url: 'http://localhost:59600/api/Values/Getc',
                method: 'Get'

            });
            m.then(function (d) {
                $scope.listc = d.data;

            });
        } filldl();
        $scope.fillddl = function () {
            fd($scope.EMP.CNAME);
        }
        function fd(s) {
            m = $http({
                url: 'http://localhost:59600/api/Values/Gets?CNAME=' + s,
                method: 'Get'

            });
            m.then(function (d) {
                $scope.lists = d.data;

            });
        }
        function fill() {
            $scope.filterOptions = {
                filterText: "",
                useExternalFilter: false
            };
            $scope.totalServerItems = 0;
            $scope.pagingOptions = {
                pageSizes: ['5', '10', '20'],
                pageSize: '5',
                currentPage: 1
            };
            $scope.setPagingData = function (data, page, pageSize) {
                var pagedData = data.slice((page - 1) * pageSize, page * pageSize);
                $scope.DataSource = pagedData;
                $scope.totalServerItems = data.length;
                if (!$scope.$$phase) {
                    $scope.$apply();
                }
            };
            $scope.getPagedDataAsync = function (pageSize, page, searchText) {
                setTimeout(function () {
                    var data;
                    if (searchText) {
                        var ft = searchText.toLowerCase();
                        $http.get('http://localhost:59600/api/Values/Gete?i=' + 1).success(function (largeLoad) {
                            data = largeLoad.filter(function (item) {
                                return JSON.stringify(item).toLowerCase().indexOf(ft) != -1;
                            });
                            $scope.setPagingData(data, page, pageSize);
                        });
                    } else {
                        $http.get('http://localhost:59600/api/Values/Gete?i=' + 2).success(function (largeLoad) {
                            $scope.setPagingData(largeLoad, page, pageSize);
                        });
                    }
                }, 100);
            };

            $scope.getPagedDataAsync($scope.pagingOptions.pageSize, $scope.pagingOptions.currentPage);

            $scope.$watch('pagingOptions', function (newVal, oldVal) {
                if (newVal !== oldVal && newVal.currentPage !== oldVal.currentPage) {
                    $scope.getPagedDataAsync($scope.pagingOptions.pageSize, $scope.pagingOptions.currentPage, $scope.filterOptions.filterText);
                }
            }, true);
            $scope.$watch('filterOptions', function (newVal, oldVal) {
                if (newVal !== oldVal) {
                    $scope.getPagedDataAsync($scope.pagingOptions.pageSize, $scope.pagingOptions.currentPage, $scope.filterOptions.filterText);
                }
            }, true);


            $scope.gridOptions = {
                data: 'DataSource',
                multiSelect: true,
                showSelectionCheckbox: true,
                enableCellEdit: false,
                enableRowSelection: true,
                enableColumnResize: false,
                enableCellSelection: true,
                enablePaging: true,
                showFooter: true,
                selectedItems: $scope.n,
                totalServerItems: 'totalServerItems',
                pagingOptions: $scope.pagingOptions,
                filterOptions: $scope.filterOptions,
                showFilter: true,
                columnDefs:
                 [
          { field: 'EID', displayName: 'Eid', width: '250px', resizable: true },
          { field: 'NAME', displayName: 'Name', width: '250px' },
           { field: "DOB", displayName: "DOB", width: "200px", cellTemplate: "<span>{{row.entity.DOB | date:'dd-MMM-yyyy'}}</span>" },

       {
           displayName: 'Actions', cellTemplate:
            '<div class="grid-action-cell">' +
            '<a ng-click="Edit(row.entity);" >Edit</a>' + ' | ' +
            '<a ng-click="Del(row.entity);" >Delete</a>' +
            '</div>'
       }
                 ],
            };


        } fill();
        $scope.Edit = function (s)
        {
            m = $http({
                url: 'http://localhost:59600/api/Values/Get?EID=' + s.EID+'&j='+1,
                method: 'Get'

            });
            m.then(function (d) {
                fd(d.data.CNAME);
                $scope.EMP = d.data;
                $scope.EMP.DOB = d.data.DOB.substring(0, 10);
            });
        }
        $scope.update = function () {
            if(k!=undefined)
            $scope.EMP.DOB = k;
            m = $http({
                url: 'http://localhost:59600/api/Values/Put',
                method: 'Put',
                data: $scope.EMP,
            });
            m.then(function (d) {
                alert(d.data);
                clr();
                fill();
            });
        }
        $scope.Del = function (s)
        {
           
            if (confirm('Do you want to delete it ?'))
            {
                m = $http({
                    url: 'http://localhost:59600/api/Values/Del?EID='+s.EID,
                    method: 'Patch',
                   
                  
                });
                m.then(function (d) {
                    alert(d.data);
                    fill();
                });

            }
        }
        $scope.reset = function ()
        {
            clr();
        }
        var p = '';
        $scope.delall = function ()
        {
            if (confirm('Do you want to delete it ?')) {
                angular.forEach($scope.n, function (i, j) {
                    p = p + 'lst=' + i.EID + '&';
                });
                p = p.substring(0, p.lastIndexOf('&'));
              
                m = $http({
                    url: 'http://localhost:59600/api/Values/Mdelete?' + p,
                    method: 'Delete'
                });
                m.then(function (d) {
                    alert(d.data);
                    fill();
                });
            }
        }

    });
   
    function emp() {
        return {
            EID: null,
            NAME: null,
            ADDRESS: null,
            DOB: null,
            GENDER: null,
            ISMARRIED: true,
            CNAME: null,
            SNAME: null

        }
    }
</script>