C#で.Net Coreの学習をしています。
ボタンを押下したときに、イベント処理(例えば、テキストフィールドに入力された内容をDBに保存)を書きたいのですが、調べても方法が分かりませんでした。
①どなたか、サンプルとともに、ご教授願えませんでしょうか。
また、以下の例の場合、Saveボタンをクリックすると、BooksController.csの
public async Task<IActionResult> Edit(int id, [Bind("id,Title,Publisher,Sample")] Book book)
の箇所が呼ばれていることは分かりましたが、なぜ呼ばれているのか分かりませんでした。
②なぜ、Saveボタンを押下した際に、上記のメソッドが呼ばれているのか、ご教示いただけますでしょうか。
上記2点について、ご回答いただけますと幸いです。
※環境
C#
Windows
ASP.Net(.NetCore 3.1)
Visual Studio
・BooksController.cs
C#
1using System; 2using System.Collections.Generic; 3using System.Linq; 4using System.Threading.Tasks; 5using Microsoft.AspNetCore.Mvc; 6using Microsoft.AspNetCore.Mvc.Rendering; 7using Microsoft.EntityFrameworkCore; 8using QuickMaster.Models; 9 10namespace QuickMaster.Controllers 11{ 12 public class BooksController : Controller 13 { 14 private readonly MyContext _context; 15 16 public BooksController(MyContext context) 17 { 18 _context = context; 19 } 20 21 // GET: Books 22 public async Task<IActionResult> Index() 23 { 24 return View(await _context.Book.ToListAsync()); 25 } 26 27 // GET: Books/Details/5 28 public async Task<IActionResult> Details(int? id) 29 { 30 if (id == null) 31 { 32 return NotFound(); 33 } 34 35 var book = await _context.Book 36 .FirstOrDefaultAsync(m => m.id == id); 37 if (book == null) 38 { 39 return NotFound(); 40 } 41 42 return View(book); 43 } 44 45 // GET: Books/Create 46 public IActionResult Create() 47 { 48 return View(); 49 } 50 51 // POST: Books/Create 52 // To protect from overposting attacks, enable the specific properties you want to bind to. 53 // For more details, see http://go.microsoft.com/fwlink/?LinkId=317598. 54 [HttpPost] 55 [ValidateAntiForgeryToken] 56 public async Task<IActionResult> Create([Bind("id,Title,Publisher,Sample")] Book book) 57 { 58 if (ModelState.IsValid) 59 { 60 _context.Add(book); 61 await _context.SaveChangesAsync(); 62 return RedirectToAction(nameof(Index)); 63 } 64 return View(book); 65 } 66 67 // GET: Books/Edit/5 68 public async Task<IActionResult> Edit(int? id) 69 { 70 if (id == null) 71 { 72 return NotFound(); 73 } 74 75 var book = await _context.Book.FindAsync(id); 76 if (book == null) 77 { 78 return NotFound(); 79 } 80 return View(book); 81 } 82 83 // POST: Books/Edit/5 84 // To protect from overposting attacks, enable the specific properties you want to bind to. 85 // For more details, see http://go.microsoft.com/fwlink/?LinkId=317598. 86 [HttpPost] 87 [ValidateAntiForgeryToken] 88 public async Task<IActionResult> Edit(int id, [Bind("id,Title,Publisher,Sample")] Book book) 89 { 90 if (id != book.id) 91 { 92 return NotFound(); 93 } 94 95 if (ModelState.IsValid) 96 { 97 try 98 { 99 _context.Update(book); 100 await _context.SaveChangesAsync(); 101 } 102 catch (DbUpdateConcurrencyException) 103 { 104 if (!BookExists(book.id)) 105 { 106 return NotFound(); 107 } 108 else 109 { 110 throw; 111 } 112 } 113 return RedirectToAction(nameof(Index)); 114 } 115 return View(book); 116 } 117 118 // GET: Books/Delete/5 119 public async Task<IActionResult> Delete(int? id) 120 { 121 if (id == null) 122 { 123 return NotFound(); 124 } 125 126 var book = await _context.Book 127 .FirstOrDefaultAsync(m => m.id == id); 128 if (book == null) 129 { 130 return NotFound(); 131 } 132 133 return View(book); 134 } 135 136 // POST: Books/Delete/5 137 [HttpPost, ActionName("Delete")] 138 [ValidateAntiForgeryToken] 139 public async Task<IActionResult> DeleteConfirmed(int id) 140 { 141 var book = await _context.Book.FindAsync(id); 142 _context.Book.Remove(book); 143 await _context.SaveChangesAsync(); 144 return RedirectToAction(nameof(Index)); 145 } 146 147 private bool BookExists(int id) 148 { 149 return _context.Book.Any(e => e.id == id); 150 } 151 } 152} 153
・Books.cs
C#
1using System; 2using System.Collections.Generic; 3using System.ComponentModel; 4using System.Linq; 5using System.Threading.Tasks; 6 7namespace QuickMaster.Models 8{ 9 public class Book 10 { 11 public int id { get; set; } 12 [DisplayName("書名")] 13 public string Title { get; set; } 14 [DisplayName("出版社")] 15 public string Price { get; set; } 16 [DisplayName("価格")] 17 public string Publisher { get; set; } 18 [DisplayName("配布サンプル")] 19 public bool Sample { get; set; } 20 } 21}
・Index.cshtml
HTML
1@model IEnumerable<QuickMaster.Models.Book> 2 3 4@{ 5 ViewData["Title"] = "Index"; 6 7} 8 9<h1>Index</h1> 10 11<p> 12 <a asp-action="Create">Create New</a> 13</p> 14<table class="table"> 15 <thead> 16 <tr> 17 <th> 18 @Html.DisplayNameFor(model => model.Title) 19 </th> 20 <th> 21 @Html.DisplayNameFor(model => model.Publisher) 22 </th> 23 <th> 24 @Html.DisplayNameFor(model => model.Sample) 25 </th> 26 <th></th> 27 </tr> 28 </thead> 29 <tbody> 30 @foreach (var item in Model) 31 { 32 <tr> 33 <td> 34 @Html.DisplayFor(modelItem => item.Title) 35 </td> 36 <td> 37 @Html.DisplayFor(modelItem => item.Publisher) 38 </td> 39 <td> 40 @Html.DisplayFor(modelItem => item.Sample) 41 </td> 42 <td> 43 <a asp-action="Edit" asp-route-id="@item.id">Edit</a> | 44 <a asp-action="Details" asp-route-id="@item.id">Details</a> | 45 <a asp-action="Delete" asp-route-id="@item.id">Delete</a> 46 </td> 47 </tr> 48 } 49 </tbody> 50</table> 51
・Edit.cshtml
HTML
1@model QuickMaster.Models.Book 2 3@{ 4 ViewData["Title"] = "Edit"; 5} 6 7<h1>Edit</h1> 8 9<h4>Book</h4> 10<hr /> 11<div class="row"> 12 <div class="col-md-4"> 13 <form asp-action="Edit"> 14 <div asp-validation-summary="ModelOnly" class="text-danger"></div> 15 <input type="hidden" asp-for="id" /> 16 <div class="form-group"> 17 <label asp-for="Title" class="control-label"></label> 18 <input asp-for="Title" class="form-control" /> 19 <span asp-validation-for="Title" class="text-danger"></span> 20 </div> 21 <div class="form-group"> 22 <label asp-for="Publisher" class="control-label"></label> 23 <input asp-for="Publisher" class="form-control" /> 24 <span asp-validation-for="Publisher" class="text-danger"></span> 25 </div> 26 <div class="form-group form-check"> 27 <label class="form-check-label"> 28 <input class="form-check-input" asp-for="Sample" /> @Html.DisplayNameFor(model => model.Sample) 29 </label> 30 </div> 31 <div class="form-group"> 32 <input type="submit" value="Save" class="btn btn-primary" /> 33 </div> 34 </form> 35 </div> 36</div> 37 38<div> 39 <a asp-action="Index">Back to List</a> 40</div> 41 42@section Scripts { 43 @{await Html.RenderPartialAsync("_ValidationScriptsPartial");} 44} 45
回答1件
あなたの回答
tips
プレビュー