Saturday, 10 February 2018

asp-action asp-route

delete link in index view
--<a asp-action="DeleteTop5" asp-route-num_to_delete="5">Delete top 5</a>--
action specify controller, route specify controller argument

num_to_delete="5" returns 5 record


num_to_delete="3" returns 3 record

num_to_delete="10" returns 10 record


history controller function

//delete few items from top
public async Task<IActionResult> DeleteTop5(int num_to_delete)
        {
            var items = await _context.History.Take(num_to_delete).ToListAsync();
            return View(items);
        }

        [HttpPost]
        [ValidateAntiForgeryToken]
        public async Task<IActionResult> DeleteTop5(IFormCollection collection)
        {
            var id_delete = collection["id_num"].ToString().Split(',');

            for (int i = 0; i < id_delete.Length - 1; i++)
            {
                var history = await _context.History.SingleOrDefaultAsync(m => m.Id == Convert.ToInt32(id_delete[i]));
                _context.History.Remove(history);
                await _context.SaveChangesAsync();
            }

            return RedirectToAction(nameof(Index));
        }

-------------------------------------------------------------------------------------------

view index.cshtml

 <a asp-action="DeleteTop5" asp-route-num_to_delete="5">Delete top 5</a>

----------------------------------------------------------------------------------------------

view delete_top_5.cshtml

@model IEnumerable<ebay_track.Models.History>

@{
    ViewData["Title"] = "Delete";
}

<script src="~/lib/jquery/dist/jquery.js"></script>
<script type="text/javascript">

    var num_array='';

    $(document).ready(function () {
        @foreach(var item in Model)
        {
        <text>
        num_array = num_array + '@item.Id' + ',';
        </text>
        }

        $("#id_num").val(num_array);
    });

</script>

<h2>Delete</h2>

<h3>Are you sure you want to delete these?</h3>
<div>
    <h4>History</h4>
    <hr />

    <table class="table">
        <thead>
            <tr>
                <th>
                    @Html.DisplayNameFor(model => model.Id)
                </th>
                <th>
                    @Html.DisplayNameFor(model => model.ActionDate)
                </th>
                <th>
                    @Html.DisplayNameFor(model => model.ActionType)
                </th>
                <th>
                    @Html.DisplayNameFor(model => model.Item)
                </th>
                <th>
                    @Html.DisplayNameFor(model => model.Tracking)
                </th>
                <th>
                    @Html.DisplayNameFor(model => model.Qty)
                </th>
                <th>
                    @Html.DisplayNameFor(model => model.ShippingDate)
                </th>
                <th>
                    @Html.DisplayNameFor(model => model.DeliveryDate)
                </th>
                <th></th>
            </tr>
        </thead>
        <tbody>
            @foreach (var item in Model)
            {
                <tr>
                    <td>
                        @Html.DisplayFor(modelItem => item.Id)
                    </td>
                    <td>
                        @Html.DisplayFor(modelItem => item.ActionDate)
                    </td>
                    <td>
                        @Html.DisplayFor(modelItem => item.ActionType)
                    </td>
                    <td>
                        @Html.DisplayFor(modelItem => item.Item)
                    </td>
                    <td>
                        @Html.DisplayFor(modelItem => item.Tracking)
                    </td>
                    <td>
                        @Html.DisplayFor(modelItem => item.Qty)
                    </td>
                    <td>
                        @Html.DisplayFor(modelItem => item.ShippingDate)
                    </td>
                    <td>
                        @Html.DisplayFor(modelItem => item.DeliveryDate)
                    </td>
                </tr>
            }
        </tbody>
    </table>

    <form method="post">
        <input type="hidden" id="id_num" name="id_num"/>
        <input type="submit" value="Delete" class="btn btn-default" /> |
        <a asp-action="Index">Back to List</a>
    </form>
</div>

Friday, 9 February 2018

阿省10大行业的工作与收入

经济变化的信息,我们一般通过加拿大统计局公布的劳动力调查(Labor Force Survey)数据了解,比如:失业率。

加拿大统计局还有一种较为传统的调查,叫做就业、薪酬和工时调查( Survey of Employment, Payroll and Hours)。这种调查能够更全面地反应就业情况,但有一些不足,比如,误差较大、按工作而不是按人进行统计。如果一个人白天做保险代理,晚上去酒吧做服务员,那他一个人所做的就要算成两份工作。

许多专家认为,加拿大统计局对于雇员薪酬(payroll)的调查,是较可靠的数据。卡尔加里大学的经济学教授Trevor Tombe说:“这方面的数据质量较高。”他说,薪酬数据的一个好处是,它能告诉我们人们实际收入有多少,包括加班收入。其缺点是,这项调查并不包括自雇者的收入。

2017年11月份的雇员薪酬数据显示,阿省约有230万份工作。以下是阿省10大行业的雇员在去年11月份的收入状况。

1.零售业:包括商场、加油站、车行等销售商品或提供售后服务的雇员。该行业阿省共有241,077份工作,雇员周收入为$629.56。

2.保健与社会服务:包括医院、老人院、托儿中心等提供保健和社会支持机构的雇员。该行业阿省共有201,240份工作,雇员周收入为$981.39。

3.建筑:阿省的建筑行业在2011年到2014年间很兴旺,但受最近这次经济衰退的冲击较大,工作数量从第二降到了第三。该行业阿省共有188,087份工作,雇员周收入为$1,446.05。

4. 餐饮住宿服务:包括饭店、餐馆、酒吧的雇员。该行业阿省共有163,787份工作,雇员周收入为$435.52。

5.教育服务:包括大、中、小学的雇员、软件、滑雪等培训机构的雇员。该行业阿省共有145,967份工作,雇员周收入为$1,093.28。

6. 制造业:该行业阿省共有122,231份工作,雇员周收入为$1,243.62。

7.专业、科学和技术服务:加拿大统计局的定义是指“几乎全靠工人技能”提供的服务,涉及面较宽,包括法律、会计、设计、工程咨询、科研、广告、公关等。该行业阿省共有114,035份工作,雇员周收入为$1,461.52。

8.公共管理:包括联邦政府、省政府、市政府机构的雇员,如当选的议员、警察、消防队员、士兵、皇家公诉人等。该行业阿省共有109,955份工作,雇员周收入为$1,399.96。

9.采矿、采油、采气和采石业:尽管人们很容易把阿省与石油和天然气开采联系起来,但工作数量仅排在第九。该行业阿省共有106,380份工作,雇员周收入为$2,219.00。

10:运输与仓储业:该行业阿省共有105,263份工作,雇员周收入为$1,214.71。

Sunday, 4 February 2018

Host ASP.NET Core on Windows with IIS


Install the .NET Core Windows Server Hosting bundle
https://aka.ms/dotnetcore-2-windowshosting



create new application pool

ebay_track app with sql access is published

change application pool for app in iis

add iis application pool to sql user

give iis read and write permision


test app working on iis on local machine

test app working on intranet



reference:
https://docs.microsoft.com/en-us/aspnet/core/host-and-deploy/iis/?tabs=aspnetcore2x

Negin Mirsalehi


https://www.youtube.com/channel/UCec6MLamEsKx8W1U8p0LgQg

Saturday, 3 February 2018

send email asp.net




firs time use external app to access gmail, google will email security alert

turn less secure app on




home controller

using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using email.Models;
using Microsoft.AspNetCore.Http;
using System.Net.Mail;
using System.Text.RegularExpressions;

namespace email.Controllers
{
    public class HomeController : Controller
    {
        public IActionResult Index()
        {
            return View();
        }

        [HttpPost]
        [ValidateAntiForgeryToken]
        public IActionResult Index(IFormCollection collection)
        {
            try
            {
                MailMessage mail = new MailMessage();
                SmtpClient SmtpServer = new SmtpClient("smtp.gmail.com");

                mail.From = new MailAddress(collection["fromAddress"].ToString() + "@gmail.com");
                mail.Subject = collection["toSubject"].ToString();
                mail.Body = collection["toMessage"].ToString();

                //add sent addresses---------------------------------------------------
                string[] addresses = collection["toAddress"].ToString().Split(';');

                for (int i = 0; i < addresses.Length; i++)
                {
                    var address = addresses[i].Replace(" ", "").Replace("\r\n","");

                    bool valid_address = false;

                    try
                    {
                        valid_address = Regex.IsMatch(address,
                              @"^(?("")("".+?(?<!\\)""@)|(([0-9a-z]((\.(?!\.))|[-!#\$%&'\*\+/=\?\^`\{\}\|~\w])*)(?<=[0-9a-z])@))" +
                              @"(?(\[)(\[(\d{1,3}\.){3}\d{1,3}\])|(([0-9a-z][-\w]*[0-9a-z]*\.)+[a-z0-9][\-a-z0-9]{0,22}[a-z0-9]))$",
                              RegexOptions.IgnoreCase, TimeSpan.FromMilliseconds(250));
                    }
                    catch (RegexMatchTimeoutException)
                    {
                        valid_address = false;
                    }

                    if (valid_address)
                    {
                        mail.To.Add(address);
                        var address_added = address + "; ";
                        ViewBag.status += address_added;
                    }
                }
                //-------------------------------------------------------------

                SmtpServer.Port = 587;
                SmtpServer.Credentials = new System.Net.NetworkCredential(collection["fromUser"].ToString(), collection["fromPassword"].ToString());
                SmtpServer.EnableSsl = true;

                SmtpServer.Send(mail);

                ViewBag.status += "email sucessfully sent";

                return View();
            }
            catch (Exception ex)
            {
                ViewBag.status = ex.ToString();
                return View();
            }
        }

        public IActionResult Error()
        {
            return View(new ErrorViewModel { RequestId = Activity.Current?.Id ?? HttpContext.TraceIdentifier });
        }
    }
}

---------------------------------------------------------------------------

home view

@{
    ViewData["Title"] = "Home Page";
}

<script src="~/lib/jquery/dist/jquery.js"></script>
<script type="text/javascript">
    $(document).ready(function () {
        $("#status").html('@ViewBag.status');

        $("#toMessage").css("resize", "none");
    });

    function fromAddress_change() {
        $("#fromUser").val($("#fromAddress").val());
    }

    function onsubmit_check() {

        if ($("#toSubject").val() == "") {

            var sentNoSubject = confirm("send without subject");

            if (sentNoSubject == true) { return true; }
            else { return false; }
        }

        return true;
    }

    $(function () { $("[data-toggle='popover']").popover(); });
</script>

<div class="text-danger" id="status"></div>

<form method="post" onsubmit="return onsubmit_check()">
    <div class="row">
        <h6>
            <label class="col-xs-2">from gmail address </label>
            <input type="text" class="col-xs-8" name="fromAddress" id="fromAddress" onchange="fromAddress_change()" required="required" />
            <label class="col-xs-2">@@gmail.com</label>
        </h6>
    </div>
    <div class="row">
        <h6>
            <label class="col-xs-2">gmail user name </label>
            <input type="text" class="col-xs-8" name="fromUser" id="fromUser" required="required" />
        </h6>
    </div>
    <div class="row">
        <h6>
            <label class="col-xs-2">gmail password </label>
            <input type="password" class="col-xs-8" name="fromPassword" required="required" />
        </h6>
    </div>
    <div class="row">
        <h6>
            <label class="col-xs-2">to email address </label>
            <textarea rows="5" class="col-xs-8" name="toAddress" required="required" placeholder="a@xyz.com; b@xyz.com; c@xyz.com"></textarea>
        </h6>
        <h3><span class="col-xs-2 glyphicon glyphicon-question-sign" title="enter multiple address" data-container="body" data-toggle="popover" data-placement="top" data-content="enter email addresses seperated by semicolon"></span></h3>
    </div>
    <div class="row">
        <h6>
            <label class="col-xs-2">email subject </label>
            <input type="text" class="col-xs-8" name="toSubject" id="toSubject" />
        </h6>
    </div>
    <div class="row">
        <h6>
            <label class="col-xs-2">message </label>
            <textarea rows="10" class="col-xs-8" name="toMessage" id="toMessage"></textarea>
        </h6>
    </div>
    <div class="row">
        <h6>
            <input type="submit" class="col-xs-8 col-xs-offset-2 btn btn-primary btn-xs" value="submit" />
        </h6>
    </div>
</form>

---------------------------------------------------------------------------------


reference:
http://csharp.net-informations.com/communications/csharp-smtp-mail.htm

bootstrap icon
https://www.w3schools.com/bootstrap/bootstrap_ref_comp_glyphs.asp

email regular expression
https://docs.microsoft.com/en-us/dotnet/standard/base-types/how-to-verify-that-strings-are-in-valid-email-format