Wednesday 26 October 2016

CURD operation in angular js using web api & modal popup


API Code:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Http;
using MvcApplication5.Models;


namespace MvcApplication5.Controllers
{
    public class EmpServiceController : ApiController
    {
        [HttpGet]
        public List<EMP> Gets()
        {
            using (Database1Entities obj = new Database1Entities())
            {
                return obj.EMPs.ToList();
            }
        }
        [HttpGet]
        public EMP Get(int Id)
        {
            using (Database1Entities obj = new Database1Entities())
            {
                return obj.EMPs.SingleOrDefault(m => m.Id == Id);
            }
        }
        [HttpPost]
        public string Post(EMP emp)
        {
            using (Database1Entities obj = new Database1Entities())
            {
                try
                {
                    obj.EMPs.Add(emp);
                    obj.SaveChanges();
                    return "Data Saved.";
                }
                catch (Exception ex)
                {
                    return ex.Message;
                }
            }
        }
        [HttpPut]
        public string Put(EMP emp)
        {
            using (Database1Entities obj = new Database1Entities())
            {
                try
                {
                    EMP emp1 = obj.EMPs.SingleOrDefault(m => m.Id == emp.Id);
                    emp1.EID = emp.EID;
                    emp1.NAME = emp.NAME;
                    obj.SaveChanges();
                    return "Data Updated.";
                }
                catch (Exception ex)
                {
                    return ex.Message;
                }
            }
        }
        [HttpDelete]
        public string Delete(int Id)
        {
            using (Database1Entities obj = new Database1Entities())
            {
                try
                {
                    EMP emp1 = obj.EMPs.SingleOrDefault(m => m.Id == Id);
                    obj.EMPs.Remove(emp1);
                    obj.SaveChanges();
                    return "Data Deleted.";
                }
                catch (Exception ex)
                {
                    return ex.Message;
                }
            }
        }
    }
}

Modal Page :



    <div class="modal-header">
        <button type="button" ng-click="cancel()" class="close" data-dismiss="modal">&times;</button>
        <h4>Emplyee Details</h4>

    </div>
    <div class="modal-body">
        <form class="form-horizontal form-width" role="form">
            <div class="form-group">
                <label class="control-label col-lg-4">EID</label>
                <div class="col-lg-4">
                    <input type="text" class="form-control" ng-model="EMP.EID" />
                </div>
            </div>
            <div class="form-group">
                <label class="control-label col-lg-4">NAME</label>
                <div class="col-lg-4">
                    <input type="text" class="form-control" ng-model="EMP.NAME" />
                </div>
            </div>
        </form>
    </div>
    <div class="modal-footer">
        <div class="form-group">
            <input type="button" class="btn btn-primary" value="Save" ng-click="save()" style="width:80px">
            <input type="button" class="btn btn-primary" value="Cancel" ng-click="cancel()" style="width:80px">
        </div>
    </div>

Index page  :

<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml" ng-app="app">
<head>
    <title></title>
    <link href="Content/bootstrap.css" rel="stylesheet" />
    <link href="Content/bootstrap-theme.css" rel="stylesheet" />
    <script src="Scripts/angular.js"></script>
    <script src="Scripts/angular-ui/ui-bootstrap-tpls.js"></script>
</head>
<body ng-controller="ctrl"><br /><br />
    <div class="container">
        <form class="form-horizontal " role="form">
            <div class="form-group">
              
                <div class="col-lg-4">
                    <input type="button" class="btn btn-primary" value="Add New" style="width:80px" ng-click="addNew()"  />
                </div>
                <label class="control-label col-lg-4"></label>
            </div>
            <div class="form-group">
                <label class="control-label col-lg-4"></label>
                <div class="col-lg-4">
                    <table class="table table-bordered table-condensed table-hover table-responsive table-striped">
                        <thead class="bg-primary">
                            <tr>
                                <th>EID</th>
                                <th>NAME</th>
                                <th>ACTION</th>
                            </tr>
                        </thead>
                        <tbody>
                            <tr ng-repeat="c in list">
                                <td>{{c.EID}}</td>
                                <td>{{c.NAME}}</td>
                                <td>
                                    <a ng-click="edit(c.Id)">Edit</a>|
                                    <a ng-click="del(c.Id)">Delete</a>
                                </td>
                            </tr>
                        </tbody>
                    </table>
                </div>
               
            </div>
        </form>
    </div>
</body>
</html>
<script type="text/javascript">
    angular.module("app", ['ui.bootstrap'])
    .controller("ctrl", function ($scope, $http, $uibModal) {
        function CLR()
        {
            $scope.EMP = new emp();
        } CLR();
        function fill()
        {
            $http.get("http://localhost:61521/api/EmpService").then(function (d) {
                $scope.list = d.data;
            });
        } fill();
        $scope.addNew = function ()
        {
            $uibModal.open({
                templateUrl: 'AddorEdit.html',
                controller: saveController,
                resolve: {
                    Id: 0
                }
            });
            
        }
        $scope.edit = function (s)
        {
            $uibModal.open({
                templateUrl: 'AddorEdit.html',
                controller: saveController,
                size: 'lg',
                resolve: {
                    Id:s
                }
            });
        }
        $scope.del = function (s)
        {
            if (confirm('Do you want to delete it ?'))
            {
                $http.delete("http://localhost:61521/api/EmpService/Delete?Id=" + s).then(function (d) {
                    alert(d.data);
                    fill();
                });
            }
        }
        var saveController = function ($scope, $http, $uibModalInstance,Id) {
            if (Id > 0)
            {
                $http.get("http://localhost:61521/api/EmpService/Get?Id="+Id).then(function (d) {
                    $scope.EMP = d.data;
                });
            }
            $scope.cancel = function ()
            {
                $uibModalInstance.dismiss();
            }
            $scope.save = function ()
            {
                if (Id == 0) {
                    $http.post("http://localhost:61521/api/EmpService/Post", $scope.EMP).then(function (d) {
                        alert(d.data);
                        fill();
                        $uibModalInstance.close();
                    });
                }
                else {
                    $http.put("http://localhost:61521/api/EmpService/Put", $scope.EMP).then(function (d) {
                        alert(d.data);
                        fill();
                        $uibModalInstance.close();
                    });
                }
               
            }
        }
    });
    function emp()
    {
        return {
            Id: null,
            EID: null,
            NAME:null
        }
    }
</script>


Wednesday 12 October 2016

Create List of CheckBoxes in AngularJS


This blog will explain you how you can create a list of CheckBoxes to store selected objects of an array in one model. I am using a directive Checklist-model which solves the task with less line of codes in controller.
First you need to download the js for Checklist-model  Press Me

service code :

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


namespace MvcApplication1.Controllers
{
    public class ValuesController : ApiController
    {
        [HttpGet]
        public List<EMP> Gets()
        {
            using (Database1Entities obj = new Database1Entities())
            {
                return obj.EMPs.ToList();
            }
        }
    }
}


view code :

<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <title></title>
    <script src="Scripts/angular.js"></script>
    <script src="Scripts/checklist-model.min.js"></script>

</head>
<body>
    <div ng-app="app">
        <div ng-controller="ctrl">
            <label ng-repeat="c in list">
                <input type="checkbox" checklist-model="selected.emps" checklist-value="c" /> {{c.NAME}}<br />
            </label>
            <button ng-click="selectAll()">Select all</button>
            <button ng-click="deselectAll()">Deselect all</button> <br />
            {{selected.emps}}
        </div>
    </div>
</body>
</html>
<script type="text/javascript">
    angular.module('app', ['checklist-model'])
    .controller('ctrl', function ($scope,$http) {
        function fill()
        {
            $http({
                url: 'http://localhost:53101/api/Values',
                type:'Get'
            }).then(function (d) {
                $scope.list = d.data;
            });
         
        } fill();
     
        $scope.selected = {
            emps: []
        };

     
        $scope.selectAll = function () {
            $scope.selected.emps = angular.copy($scope.list);
        };

     
        $scope.deselectAll = function () {
            $scope.selected.emps = [];
        };
    });
</script>

Create List of CheckBoxes in AngularJS


This blog will explain you how you can create a list of CheckBoxes to store selected objects of an array in one model. I am using a directive Checklist-model which solves the task with less line of codes in controller.
First you need to download the js for Checklist-model  Press Me

service code :

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


namespace MvcApplication1.Controllers
{
    public class ValuesController : ApiController
    {
        [HttpGet]
        public List<EMP> Gets()
        {
            using (Database1Entities obj = new Database1Entities())
            {
                return obj.EMPs.ToList();
            }
        }
    }
}


view code :

<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <title></title>
    <script src="Scripts/angular.js"></script>
    <script src="Scripts/checklist-model.min.js"></script>

</head>
<body>
    <div ng-app="app">
        <div ng-controller="ctrl">
            <label ng-repeat="c in list">
                <input type="checkbox" checklist-model="selected.emps" checklist-value="c" /> {{c.NAME}}<br />
            </label>
            <button ng-click="selectAll()">Select all</button>
            <button ng-click="deselectAll()">Deselect all</button> <br />
            {{selected.emps}}
        </div>
    </div>
</body>
</html>
<script type="text/javascript">
    angular.module('app', ['checklist-model'])
    .controller('ctrl', function ($scope,$http) {
        function fill()
        {
            $http({
                url: 'http://localhost:53101/api/Values',
                type:'Get'
            }).then(function (d) {
                $scope.list = d.data;
            });
         
        } fill();
     
        $scope.selected = {
            emps: []
        };

     
        $scope.selectAll = function () {
            $scope.selected.emps = angular.copy($scope.list);
        };

     
        $scope.deselectAll = function () {
            $scope.selected.emps = [];
        };
    });
</script>

Textarea issue in chrome.

<textarea name="txtarea"  class="form-control" style="resize:none"></textarea>

Friday 7 October 2016

how to send mail in MVC

first create HTML file
Equipment Installation Failure
Hello <<ServiceManagername>>,<br><br>
<p>This is to notify you that following piece of equipment has failed the installation test performed at <<wlocation>>. </p>
<p>Please find the equipment details below:</p>
<table width="100%" border="1" cellspacing="0" cellpadding="10" style="border:1px solid #555; font-family:Arial, sans-serif; font-size:14px">
<thead>
                        <tr style='background-color:silver'>
                            <th style="color:#1f497f; padding:5px 0; font-family:Arial, sans-serif; font-size:14px;">Asset No</th>
                            <th style="color:#1f497f; padding:5px 0; font-family:Arial, sans-serif; font-size:14px">Model Name</th>
                            <th style="color:#1f497f; padding:5px 0; font-family:Arial, sans-serif; font-size:14px">Serial Number</th>
                            <th style="color:#1f497f; padding:5px 0; font-family:Arial, sans-serif; font-size:14px">Performed By</th>
                            <th style="color:#1f497f; padding:5px 0; font-family:Arial, sans-serif; font-size:14px">Performed On</th>
                          <th style="color:#1f497f; padding:5px 0; font-family:Arial, sans-serif; font-size:14px">Status</th>
                        </tr>
</thead>

<tr style="text-align:center">
<td><<AssetNo>></td>
<td><<ModelName>></td>
<td><<SerialNumber>></td>
<td><<PerformedBy>></td>
<td><<Performedon>></td>
        <td><<Status>></td>
</tr>
</table>
<br>
<p>To know the equipment details and its location history, please <a href='<<path>>'>Login</a></p>  
<br />
Thanks,<br />
Corporate Essentials


C # CODE :

 if (sid == 19)
                {
                    string EmailSystemName = WebConfigurationManager.AppSettings["DefaultEmailSystemName"].ToString();
                    EmailTemplate emailTemplate = new EmailTemplate();
                    emailTemplate.emailAccount = _EmailAccountService.GetEmailAccountBySystemName(EmailSystemName);
                    string txtFileName = Server.MapPath("~/Administration/Content/HTMLTemplates/barcodeInstallation.txt");
                    string[] lines;
                    string mailSubject = "";
                    string MailBody = "";
                    if (System.IO.File.Exists(txtFileName))
                    {
                        lines = System.IO.File.ReadAllLines(txtFileName);
                        mailSubject = lines[0];
                        var otherLines = new ArraySegment<String>(lines, 1, lines.Length - 1);
                        MailBody = String.Join("", otherLines);
                    }
                    var customer = EngineContext.Current.Resolve<IWorkContext>().CurrentCustomer;
                    List<Customer> customerList = _customerService.GetAllCustomers(0, int.MaxValue, string.Empty).Where(c => c.CustomerRoles.Select(cr => cr.SystemName).Contains("CustomerServiceManager")).ToList();
                    foreach (Customer singleCustomer in customerList)
                    {
                        emailTemplate.fromAddress = customer.Email;
                        emailTemplate.fromName = "Corporate Essentials";
                        emailTemplate.toAddress = singleCustomer.Email;
                        string mailBody = MailBody;
                        mailBody = mailBody.Replace("<<ServiceManagername>>", singleCustomer.GetFullName() ?? string.Empty);
                        mailBody = mailBody.Replace("<<wlocation>>", mailLocation ?? string.Empty);
                        mailBody = mailBody.Replace("<<AssetNo>>", equipment.AssetID ?? string.Empty);
                        mailBody = mailBody.Replace("<<ModelName>>", equipment.Model.Name ?? string.Empty);
                        mailBody = mailBody.Replace("<<SerialNumber>>", equipment.SerialNo ?? string.Empty);
                        mailBody = mailBody.Replace("<<PerformedBy>>", EngineContext.Current.Resolve<IWorkContext>().CurrentCustomer.GetFullName() ?? string.Empty);
                        mailBody = mailBody.Replace("<<Performedon>>", DateTime.UtcNow.ToShortDateString() ?? string.Empty);
                        mailBody = mailBody.Replace("<<Status>>", status ?? string.Empty);
                        mailBody = mailBody.Replace("<<path>>", Request.Url.AbsoluteUri ?? string.Empty);
                        emailTemplate.replyToName = "Corporate Essentials";
                        emailTemplate.subject = mailSubject;
                        emailTemplate.body = mailBody;
                        _EmailSender.SendEmail(emailTemplate);
                    }
                }
                

Wednesday 5 October 2016

AngularJS Custom Directives

@{
    ViewBag.Title = "Index";
}


<script src="~/Scripts/angular.js"></script>
<div ng-app="app" ng-controller="ctr">
    <temp-Home name="siv"></temp-Home>
</div>
<script type="text/javascript">
    angular.module("app", [])
    .controller("ctr", function ($scope) {
        $scope.siv = {};
        $scope.siv.FN = "Siv ";
        $scope.siv.LN = "Sankar";
    })
    .directive("tempHome", function () {
        var directive = {};
        directive.restrict = "E";
        directive.scope={
        user:"=name"
        };
        directive.template = "User : <b>{{user.FN}}</b><b>{{user.LN}}</b>";
       
        directive.link = function ($scope, element, attributes) {
            element.html("This is the new content: " +  $scope.siv.FN);
            element.css("background-color", "#ffff00");
        };

        directive.compile = function (element, attributes) {
            element.css("border", "5px solid #ABCABC");
        };

        //directive.compile = function (element, attributes) {
        //    element.css("border", "1px solid #cccccc");

        //    var linkFunction = function ($scope, element, attributes) {
        //        element.html("This is the new content: " + $scope.siv);
        //        element.css("background-color", "#ffff00");
        //    }

        //    return linkFunction;
        //}
        return directive;
    });
</script>