- 投稿日:2021-01-19T21:11:36+09:00
【C#入門】初学者がASP.NETでWebアプリを作る:第2回
今回やること
・前回スキャフォールディングでうまくできなかった原因を解消
・Viewの作りこみ前回ダメだったところ
①導入するフレームワークが違ったらしい
Npgsqlは合っているけど、Postgres用のEntity FrameworkのNpgsqlを入れなければいけなかったらしい。
参考:https://qiita.com/nosa67/items/49ac036a15a4f7deed8bフレームワークを入れ替えて、下記のように修正。
appsettings.json{ "Logging": { "LogLevel": { "Default": "Information", "Microsoft": "Warning", "Microsoft.Hosting.Lifetime": "Information" } }, "AllowedHosts": "*", "ConnectionStrings": { //"SalaryManagementSystemContext": "Server=(localdb)\\mssqllocaldb;Database=SalaryManagementSystemContext-265af9c0-8ccd-465a-b2a3-0aea1030624b;Trusted_Connection=True;MultipleActiveResultSets=true" "DefaultConnection": "Server=localhost;Port=5432;Database=SMSDB;User ID=postgres;Password=password;Enlist=true" } }Startup.cspublic void ConfigureServices(IServiceCollection services) { services.AddControllersWithViews(); services.AddDbContext<SalaryManagementSystemContext>(options => //options.UseSqlServer(Configuration.GetConnectionString("SalaryManagementSystemContext"))); options.UseNpgsql(Configuration.GetConnectionString("DefaultConnection"))); }②そもそもASP.NET Coreのバージョンがダメだったらしい
何かのライブラリのサポートバージョンが合っていなかったので、5.0で再作成…
③前回のControllerを作るまで実施
すんなりいけた。
①を再実施、ただしDataフォルダは削除してはいけない。
※DBContextの継承クラスを使うため。
参考資料のUpdate-Databaseコマンドの実行まで行い、pgAdminでSalaryテーブルができていることを確認。
実行してみる
この時点で気に入らないところをちょっと修正
①Viewを直す
Chromeがご丁寧に翻訳してくれちゃうので日本語扱いにする。
_Layout.cshtml<!DOCTYPE html> <html lang="ja">②項目名をちゃんと設定する
ついでに必須チェックも
Salary.csusing System; using System.Collections.Generic; using System.ComponentModel.DataAnnotations; using System.Linq; using System.Threading.Tasks; namespace SalaryManagementSystem.Models { public class Salary { // ID public int Id { get; set; } // 受給者 [Display(Name ="受給者")] [Required(ErrorMessage = "受給者は必須入力です")] public string Beneficiary { get; set; } // タイプ(給与、賞与) [Display(Name = "給与/賞与")] [Required(ErrorMessage = "給与/賞与は必須入力です")] public string PaymentType { get; set; } // 支給日 [Display(Name = "支給日")] [Required(ErrorMessage = "支給日は必須入力です")] public DateTime PaymentDate { get; set; } // 支給額 [Display(Name = "支給額")] [Required(ErrorMessage = "支給額は必須入力です")] public decimal PaymentAmount { get; set; } // 交通費 [Display(Name = "交通費")] public decimal TravelExpence { get; set; } // 健康保険料 [Display(Name = "健康保険料")] public decimal HealthInsurancePremium { get; set; } // 厚生年金料 [Display(Name = "厚生年金料")] public decimal WelfarePension { get; set; } // 雇用保険料 [Display(Name = "雇用保険料")] public decimal EmploymentInsurancePremium { get; set; } // 所得税 [Display(Name = "所得税")] public decimal IncomeTax { get; set; } // 住民税 [Display(Name = "住民税")] public decimal ResidentTax { get; set; } // 総支給 [Display(Name = "総支給額")] public decimal TotalPaymentAmount { get; set; } // 時間外手当 [Display(Name = "時間外手当")] public decimal OvertimeAllowance { get; set; } // 深夜手当 [Display(Name = "深夜手当")] public decimal MidnightAllowance { get; set; } // 休日手当 [Display(Name = "休日手当")] public decimal HolidayAllowance { get; set; } // 備考 [Display(Name = "備考")] public string Remarks { get; set; } // 登録日時 public DateTime RegisterDate { get; set; } // 登録ユーザID public string RegisterUser { get; set; } // 更新日時 public DateTime UpdateDate { get; set; } // 更新ユーザID public string UpdateUser { get; set; } } }③非表示項目を設定する
登録日時・登録ユーザID・更新日時・更新ユーザIDは管理用の項目なので、画面には表示しないようにする。
追加させられたIdは勝手に非表示になるんだ…
ついでに日本語化。
日本語化と非表示項目の削除は、必要な画面すべてで実施する。Index.cshtml@model IEnumerable<SalaryManagementSystem.Models.Salary> @{ ViewData["Title"] = "一覧"; ←日本語化 } <h1>一覧</h1> ←日本語化 <p> <a asp-action="Create">データ登録</a> ←日本語化 </p> <table class="table"> <thead> <tr> <th> @Html.DisplayNameFor(model => model.Beneficiary) </th> <th> @Html.DisplayNameFor(model => model.PaymentType) </th> <th> @Html.DisplayNameFor(model => model.PaymentDate) </th> <th> @Html.DisplayNameFor(model => model.PaymentAmount) </th> <th> @Html.DisplayNameFor(model => model.TravelExpence) </th> <th> @Html.DisplayNameFor(model => model.HealthInsurancePremium) </th> <th> @Html.DisplayNameFor(model => model.WelfarePension) </th> <th> @Html.DisplayNameFor(model => model.EmploymentInsurancePremium) </th> <th> @Html.DisplayNameFor(model => model.IncomeTax) </th> <th> @Html.DisplayNameFor(model => model.ResidentTax) </th> <th> @Html.DisplayNameFor(model => model.TotalPaymentAmount) </th> <th> @Html.DisplayNameFor(model => model.OvertimeAllowance) </th> <th> @Html.DisplayNameFor(model => model.MidnightAllowance) </th> <th> @Html.DisplayNameFor(model => model.HolidayAllowance) </th> <th> @Html.DisplayNameFor(model => model.Remarks) </th> @*<th> ←*.cshtmlのコメントアウトは「@*」で開始、「*@」で終了 @Html.DisplayNameFor(model => model.RegisterDate) </th> <th> @Html.DisplayNameFor(model => model.RegisterUser) </th> <th> @Html.DisplayNameFor(model => model.UpdateDate) </th> <th> @Html.DisplayNameFor(model => model.UpdateUser) </th>*@ <th></th> </tr> </thead> <tbody> @foreach (var item in Model) { <tr> <td> @Html.DisplayFor(modelItem => item.Beneficiary) </td> <td> @Html.DisplayFor(modelItem => item.PaymentType) </td> <td> @Html.DisplayFor(modelItem => item.PaymentDate) </td> <td> @Html.DisplayFor(modelItem => item.PaymentAmount) </td> <td> @Html.DisplayFor(modelItem => item.TravelExpence) </td> <td> @Html.DisplayFor(modelItem => item.HealthInsurancePremium) </td> <td> @Html.DisplayFor(modelItem => item.WelfarePension) </td> <td> @Html.DisplayFor(modelItem => item.EmploymentInsurancePremium) </td> <td> @Html.DisplayFor(modelItem => item.IncomeTax) </td> <td> @Html.DisplayFor(modelItem => item.ResidentTax) </td> <td> @Html.DisplayFor(modelItem => item.TotalPaymentAmount) </td> <td> @Html.DisplayFor(modelItem => item.OvertimeAllowance) </td> <td> @Html.DisplayFor(modelItem => item.MidnightAllowance) </td> <td> @Html.DisplayFor(modelItem => item.HolidayAllowance) </td> <td> @Html.DisplayFor(modelItem => item.Remarks) </td> @*<td> @Html.DisplayFor(modelItem => item.RegisterDate) </td> <td> @Html.DisplayFor(modelItem => item.RegisterUser) </td> <td> @Html.DisplayFor(modelItem => item.UpdateDate) </td> <td> @Html.DisplayFor(modelItem => item.UpdateUser) </td>*@ <td> <a asp-action="Edit" asp-route-id="@item.Id">編集</a> | ←日本語化 <a asp-action="Details" asp-route-id="@item.Id">詳細</a> | ←日本語化 <a asp-action="Delete" asp-route-id="@item.Id">削除</a> ←日本語化 </td> </tr> } </tbody> </table>見た目はいったんOK
次回予告
次回は登録処理の作りこみとアカウント認証機能の追加をやります。
- 投稿日:2021-01-19T17:13:54+09:00
【C#】新しいフォルダーをたくさん作るお遊びプログラム
はじめに
みなさんは「木を隠すなら森の中」という言葉をご存じでしょうか。
誰しも、人に見られたくないデータってありますよね?
そんなデータを隠すにはどうしたらいいのか!!それは!!
たくさんのフォルダーの海の中に隠せばいいのだ!!!(安直)ということで、デスクトップにフォルダーをひとつ作り、その中に大量の「新しいフォルダー」を作るプログラムを書きました。
単なるお遊びプログラムです('ω')
使い道は…知らぬ('ω')コード
createfolder.csusing System; using System.Collections.Generic; using System.IO; namespace createfolder { class Program { static string BasePath = System.Environment.GetFolderPath(Environment.SpecialFolder.DesktopDirectory) + @"\ManyFolder\"; static void Main(string[] args) { Console.WriteLine("フォルダーを作成します。"); Console.WriteLine("作成先:「" + BasePath + "」"); Console.WriteLine("フォルダーを作成しています。"); double MaxFolder = 9999; //実際のフォルダ数はMaxFolder設定値+1となる。 List<double> list = new List<double>() { 0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8, 0.9, 1.0 }; List<int> PercentValueList = new List<int>(); int listCount = 0; while(true) { PercentValueList.Add(Convert.ToInt32(MaxFolder * list[listCount])); if(listCount == 9) { break; } listCount++; } int i = 0; while(true) { if(i == 0) { Directory.CreateDirectory(BasePath + "新しいフォルダー"); //作成日時の設定(現在の時間にする) System.IO.Directory.SetCreationTime(BasePath + "新しいフォルダー", DateTime.Now); //更新日時の設定 System.IO.Directory.SetLastWriteTime(BasePath + "新しいフォルダー", DateTime.Now); //アクセス日時の設定 System.IO.Directory.SetLastAccessTime(BasePath + "新しいフォルダー", DateTime.Now); } else if(i >= 1) { Directory.CreateDirectory(BasePath + "新しいフォルダー(" + i + ")"); //作成日時の設定(現在の時間にする) System.IO.Directory.SetCreationTime(BasePath + "新しいフォルダー(" + i + ")", DateTime.Now); //更新日時の設定 System.IO.Directory.SetLastWriteTime(BasePath + "新しいフォルダー(" + i + ")", DateTime.Now); //アクセス日時の設定 System.IO.Directory.SetLastAccessTime(BasePath + "新しいフォルダー(" + i + ")", DateTime.Now); } for(int j = 0; j < 9; j++) { if(i == PercentValueList[j]) { switch(j) { case 0: Console.WriteLine("10%"); break; case 1: Console.WriteLine("20%"); break; case 2: Console.WriteLine("30%"); break; case 3: Console.WriteLine("40%"); break; case 4: Console.WriteLine("50%"); break; case 5: Console.WriteLine("60%"); break; case 6: Console.WriteLine("70%"); break; case 7: Console.WriteLine("80%"); break; case 8: Console.WriteLine("90%"); break; case 9: Console.WriteLine("100%"); break; } } } if(i == MaxFolder) { break; } i++; } Console.WriteLine("100%"); Console.WriteLine("フォルダーを作成しました。何かキーを押して終了してください。"); Console.ReadKey(); } } }