SkyoceanHome
Home
Privacy
ダウンロード
SkyShop
Updated Information
チュートリアルコード集
References
記事編集
日付
ジャンルのカテゴリ―名
名前を選択して下さい。
Contexts
Controllers
Databases
Htmls
JavaScripts
Models
MVCTutorials
Others
Styles
Views
タイトル
内容の要旨
<p style="border: 0px; border-collapse: collapse; border-spacing: 0px; list-style-position: initial; list-style-image: initial; list-style-type: revert; margin: 4px; padding: revert; -webkit-line-clamp: revert; color: rgba(0, 0, 0, 0.8); font-family: roboto, helvetica, sans-serif; font-size: 16px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; white-space: normal; background-color: rgb(255, 255, 255); text-decoration-thickness: initial; text-decoration-style: initial; text-decoration-color: initial;" data-pasted="true">了解しました。<br>先ほどのサンプルを、より実用的で堅牢な形に改良して再提示します。<br>以下では、<strong style="font-weight: 700;">ASP.NET Core MVC + Entity Framework Core</strong> を使い、View 側の <code style="border-radius: 8px; background: none 0% 0% / auto repeat scroll padding-box border-box rgb(255, 255, 255);"><script></code> から Ajax で Controller の更新アクションを呼び出す構成です。<br>前回よりも <strong style="font-weight: 700;">バリデーション強化</strong>、<strong style="font-weight: 700;">エラー処理の詳細化</strong>、<strong style="font-weight: 700;">SweetAlert2 との統合例</strong> を追加しています。</p><h2 style="border: 0px; border-collapse: collapse; border-spacing: 0px; list-style-position: initial; list-style-image: initial; list-style-type: revert; margin: revert; padding: revert; font: 22px / 24px roboto, helvetica, sans-serif; height: 32px; -webkit-line-clamp: 1; overflow: hidden; text-overflow: ellipsis; display: -webkit-box; -webkit-box-orient: vertical; color: rgba(0, 0, 0, 0.8); letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; white-space: normal; background-color: rgb(255, 255, 255); text-decoration-thickness: initial; text-decoration-style: initial; text-decoration-color: initial;"><strong style="font-weight: 700;">1. Controller 側(更新用アクション)</strong></h2><pre data-priority="2" style="background: none 0% 0% / auto repeat scroll padding-box border-box rgb(245, 245, 245); border: 1px solid rgba(0, 0, 0, 0.1); border-radius: 8px; overflow: hidden; color: rgba(0, 0, 0, 0.8); font-size: 16px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-thickness: initial; text-decoration-style: initial; text-decoration-color: initial;"><div data-bm="24" style="display: flex; height: 28px; flex-direction: row; justify-content: space-between; align-items: center; gap: 8px; padding: 8px 12px; border-bottom: 1px solid rgba(0, 0, 0, 0.1); background: none 0% 0% / auto repeat scroll padding-box border-box rgb(243, 249, 254);"><h5 style="border: 0px; border-collapse: collapse; border-spacing: 0px; list-style-position: initial; list-style-image: initial; list-style-type: revert; margin: 0px; padding: revert; font-style: normal !important; font-variant: normal !important; font-weight: 400 !important; font-stretch: 100% !important; line-height: 18px; font-family: roboto, helvetica, sans-serif !important; font-optical-sizing: auto !important; font-size-adjust: none !important; font-kerning: auto !important; font-feature-settings: normal !important; font-variation-settings: normal !important; font-language-override: normal !important; font-size: 16px; display: inline-block; color: rgb(11, 73, 127);">Csharp</h5><div style="height: 28px;"><button type="button" tabindex="" title="Copy code" style="box-sizing: border-box; margin: 0px; padding: 0px; font: 400 13px / 20px roboto, helvetica, sans-serif; background-color: rgb(225, 240, 253); border: 1px solid rgba(0, 0, 0, 0); display: grid; grid-auto-flow: column; align-items: center; cursor: pointer; color: rgb(0, 0, 0) !important; text-decoration: none !important; border-radius: 9999px; block-size: 28px; gap: 2px; padding-inline: 10px;"><div style="box-sizing: border-box; margin: 0px; padding: 0px; min-inline-size: 0px; max-inline-size: 100%; white-space: nowrap; text-overflow: ellipsis; overflow: clip; max-block-size: calc(1lh); padding-inline: 2px;">Copy code</div></button></div></div><code style="border-radius: 8px; background: none 0% 0% / auto repeat scroll padding-box border-box rgb(255, 255, 255); display: block; white-space: pre-wrap; overflow-wrap: anywhere; padding: 8px 16px 16px;"><span style="color: rgb(215, 58, 73);">using</span> Microsoft.AspNetCore.Mvc; <span style="color: rgb(215, 58, 73);">using</span> Microsoft.EntityFrameworkCore; <span style="color: rgb(215, 58, 73);">using</span> System.Threading.Tasks; <span style="color: rgb(215, 58, 73);">using</span> YourAppNamespace.Data; <span style="color: rgb(106, 115, 125);">// DbContext の名前空間</span> <span style="color: rgb(215, 58, 73);">using</span> YourAppNamespace.Models; <span style="color: rgb(106, 115, 125);">// Payment モデルの名前空間</span> <span style="color: rgb(215, 58, 73);">public</span> <span style="color: rgb(215, 58, 73);">class</span> <span style="color: rgb(111, 66, 193);">PaymentsController</span> : <span style="color: rgb(111, 66, 193);">Controller</span> { <span style="color: rgb(215, 58, 73);">private</span> <span style="color: rgb(215, 58, 73);">readonly</span> ApplicationDbContext _context; <span style="color: rgb(215, 58, 73);">public</span> <span style="color: rgb(111, 66, 193);">PaymentsController</span>(ApplicationDbContext context) { _context = context; } <span style="color: rgb(106, 115, 125);"><span style="color: rgb(215, 58, 73);">///</span> <span style="color: rgb(215, 58, 73);"><summary></span></span> <span style="color: rgb(106, 115, 125);"><span style="color: rgb(215, 58, 73);">///</span> Ajax から呼び出される Payment 更新アクション</span> <span style="color: rgb(106, 115, 125);"><span style="color: rgb(215, 58, 73);">///</span> <span style="color: rgb(215, 58, 73);"></summary></span></span> [<span style="color: rgb(0, 92, 197);">HttpPost</span>] [<span style="color: rgb(0, 92, 197);">ValidateAntiForgeryToken</span>] <span style="color: rgb(106, 115, 125);">// CSRF 対策</span> <span style="color: rgb(215, 58, 73);">public</span> <span style="color: rgb(215, 58, 73);">async</span> Task<IActionResult> <span style="color: rgb(111, 66, 193);">UpdatePayment</span>([FromBody] PaymentUpdateDto dto) { <span style="color: rgb(215, 58, 73);">if</span> (dto == <span style="color: rgb(0, 92, 197);">null</span> || dto.PaymentId <= <span style="color: rgb(0, 92, 197);">0</span>) { <span style="color: rgb(215, 58, 73);">return</span> BadRequest(<span style="color: rgb(215, 58, 73);">new</span> { message = <span style="color: rgb(3, 47, 98);">"不正なデータです。"</span> }); } <span style="color: rgb(215, 58, 73);">var</span> payment = <span style="color: rgb(215, 58, 73);">await</span> _context.Payments.FirstOrDefaultAsync(p => p.PaymentId == dto.PaymentId); <span style="color: rgb(215, 58, 73);">if</span> (payment == <span style="color: rgb(0, 92, 197);">null</span>) { <span style="color: rgb(215, 58, 73);">return</span> NotFound(<span style="color: rgb(215, 58, 73);">new</span> { message = <span style="color: rgb(3, 47, 98);">"指定された支払いデータが見つかりません。"</span> }); } <span style="color: rgb(106, 115, 125);">// 値の更新</span> payment.IncomeCategoryId = dto.IncomeCategoryId; payment.Amount = dto.Amount; <span style="color: rgb(215, 58, 73);">try</span> { <span style="color: rgb(215, 58, 73);">await</span> _context.SaveChangesAsync(); <span style="color: rgb(215, 58, 73);">return</span> Ok(<span style="color: rgb(215, 58, 73);">new</span> { message = <span style="color: rgb(3, 47, 98);">"更新が完了しました。"</span> }); } <span style="color: rgb(215, 58, 73);">catch</span> (DbUpdateException ex) { <span style="color: rgb(215, 58, 73);">return</span> StatusCode(<span style="color: rgb(0, 92, 197);">500</span>, <span style="color: rgb(215, 58, 73);">new</span> { message = <span style="color: rgb(3, 47, 98);">"データベース更新に失敗しました。"</span>, error = ex.Message }); } } } <span style="color: rgb(106, 115, 125);"><span style="color: rgb(215, 58, 73);">///</span> <span style="color: rgb(215, 58, 73);"><summary></span></span> <span style="color: rgb(106, 115, 125);"><span style="color: rgb(215, 58, 73);">///</span> Ajax で受け取る更新用 DTO</span> <span style="color: rgb(106, 115, 125);"><span style="color: rgb(215, 58, 73);">///</span> <span style="color: rgb(215, 58, 73);"></summary></span></span> <span style="color: rgb(215, 58, 73);">public</span> <span style="color: rgb(215, 58, 73);">class</span> <span style="color: rgb(111, 66, 193);">PaymentUpdateDto</span> { <span style="color: rgb(215, 58, 73);">public</span> <span style="color: rgb(227, 98, 9);">int</span> PaymentId { <span style="color: rgb(215, 58, 73);">get</span>; <span style="color: rgb(215, 58, 73);">set</span>; } <span style="color: rgb(215, 58, 73);">public</span> <span style="color: rgb(227, 98, 9);">int</span> IncomeCategoryId { <span style="color: rgb(215, 58, 73);">get</span>; <span style="color: rgb(215, 58, 73);">set</span>; } <span style="color: rgb(215, 58, 73);">public</span> <span style="color: rgb(227, 98, 9);">decimal</span> Amount { <span style="color: rgb(215, 58, 73);">get</span>; <span style="color: rgb(215, 58, 73);">set</span>; } } </code></pre><h2 style="border: 0px; border-collapse: collapse; border-spacing: 0px; list-style-position: initial; list-style-image: initial; list-style-type: revert; margin: revert; padding: revert; font: 22px / 24px roboto, helvetica, sans-serif; height: 32px; -webkit-line-clamp: 1; overflow: hidden; text-overflow: ellipsis; display: -webkit-box; -webkit-box-orient: vertical; color: rgba(0, 0, 0, 0.8); letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; white-space: normal; background-color: rgb(255, 255, 255); text-decoration-thickness: initial; text-decoration-style: initial; text-decoration-color: initial;"><strong style="font-weight: 700;">2. View 側 JavaScript(SweetAlert2 + Ajax 呼び出し)</strong></h2><pre data-priority="2" style="background: none 0% 0% / auto repeat scroll padding-box border-box rgb(245, 245, 245); border: 1px solid rgba(0, 0, 0, 0.1); border-radius: 8px; overflow: hidden; color: rgba(0, 0, 0, 0.8); font-size: 16px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-thickness: initial; text-decoration-style: initial; text-decoration-color: initial;"><div data-bm="25" style="display: flex; height: 28px; flex-direction: row; justify-content: space-between; align-items: center; gap: 8px; padding: 8px 12px; border-bottom: 1px solid rgba(0, 0, 0, 0.1); background: none 0% 0% / auto repeat scroll padding-box border-box rgb(243, 249, 254);"><h5 style="border: 0px; border-collapse: collapse; border-spacing: 0px; list-style-position: initial; list-style-image: initial; list-style-type: revert; margin: 0px; padding: revert; font-style: normal !important; font-variant: normal !important; font-weight: 400 !important; font-stretch: 100% !important; line-height: 18px; font-family: roboto, helvetica, sans-serif !important; font-optical-sizing: auto !important; font-size-adjust: none !important; font-kerning: auto !important; font-feature-settings: normal !important; font-variation-settings: normal !important; font-language-override: normal !important; font-size: 16px; display: inline-block; color: rgb(11, 73, 127);">Html</h5><div style="height: 28px;"><button type="button" tabindex="" title="Copy code" style="box-sizing: border-box; margin: 0px; padding: 0px; font: 400 13px / 20px roboto, helvetica, sans-serif; background-color: rgb(225, 240, 253); border: 1px solid rgba(0, 0, 0, 0); display: grid; grid-auto-flow: column; align-items: center; cursor: pointer; color: rgb(0, 0, 0) !important; text-decoration: none !important; border-radius: 9999px; block-size: 28px; gap: 2px; padding-inline: 10px;"><div style="box-sizing: border-box; margin: 0px; padding: 0px; min-inline-size: 0px; max-inline-size: 100%; white-space: nowrap; text-overflow: ellipsis; overflow: clip; max-block-size: calc(1lh); padding-inline: 2px;">Copy code</div></button></div></div><code style="border-radius: 8px; background: none 0% 0% / auto repeat scroll padding-box border-box rgb(255, 255, 255); display: block; white-space: pre-wrap; overflow-wrap: anywhere; padding: 8px 16px 16px;"><<span style="color: rgb(34, 134, 58);">script</span> <span style="color: rgb(0, 92, 197);">src</span>=<span style="color: rgb(3, 47, 98);">"https://code.jquery.com/jquery-3.6.0.min.js"</span>></<span style="color: rgb(34, 134, 58);">script</span>> <<span style="color: rgb(34, 134, 58);">script</span> <span style="color: rgb(0, 92, 197);">src</span>=<span style="color: rgb(3, 47, 98);">"https://cdn.jsdelivr.net/npm/sweetalert2@11"</span>></<span style="color: rgb(34, 134, 58);">script</span>> <<span style="color: rgb(34, 134, 58);">script</span>> <span style="color: rgb(106, 115, 125);">// 例: SweetAlert2 の confirm 後に Ajax 更新</span> }).<span style="color: rgb(111, 66, 193);">then</span>((result) => { <span style="color: rgb(215, 58, 73);">if</span> (result.isConfirmed && paymentId && paymentId > <span style="color: rgb(0, 92, 197);">0</span>) { <span style="color: rgb(215, 58, 73);">const</span> data = { <span style="color: rgb(0, 92, 197);">paymentId</span>: paymentId, <span style="color: rgb(0, 92, 197);">incomeCategoryId</span>: result.value.incomeCategoryId, <span style="color: rgb(0, 92, 197);">amount</span>: result.value.amount }; $.<span style="color: rgb(111, 66, 193);">ajax</span>({ <span style="color: rgb(0, 92, 197);">url</span>: <span style="color: rgb(3, 47, 98);">'/Payments/UpdatePayment'</span>, <span style="color: rgb(0, 92, 197);">type</span>: <span style="color: rgb(3, 47, 98);">'POST'</span>, <span style="color: rgb(0, 92, 197);">contentType</span>: <span style="color: rgb(3, 47, 98);">'application/json'</span>, <span style="color: rgb(0, 92, 197);">headers</span>: { <span style="color: rgb(3, 47, 98);">'RequestVerificationToken'</span>: $(<span style="color: rgb(3, 47, 98);">'input[name="__RequestVerificationToken"]'</span>).<span style="color: rgb(111, 66, 193);">val</span>() <span style="color: rgb(106, 115, 125);">// CSRF 対策</span> }, <span style="color: rgb(0, 92, 197);">data</span>: <span style="color: rgb(111, 66, 193);">JSON</span>.<span style="color: rgb(111, 66, 193);">stringify</span>(data), <span style="color: rgb(0, 92, 197);">success</span>: <span style="color: rgb(215, 58, 73);">function</span> (response) { <span style="color: rgb(111, 66, 193);">Swal</span>.<span style="color: rgb(111, 66, 193);">fire</span>({ <span style="color: rgb(0, 92, 197);">icon</span>: <span style="color: rgb(3, 47, 98);">'success'</span>, <span style="color: rgb(0, 92, 197);">title</span>: <span style="color: rgb(3, 47, 98);">'成功'</span>, <span style="color: rgb(0, 92, 197);">text</span>: response.message }).<span style="color: rgb(111, 66, 193);">then</span>(() => { location.<span style="color: rgb(111, 66, 193);">reload</span>(); <span style="color: rgb(106, 115, 125);">// 必要に応じて画面更新</span> }); }, <span style="color: rgb(0, 92, 197);">error</span>: <span style="color: rgb(215, 58, 73);">function</span> (xhr) { <span style="color: rgb(111, 66, 193);">Swal</span>.<span style="color: rgb(111, 66, 193);">fire</span>({ <span style="color: rgb(0, 92, 197);">icon</span>: <span style="color: rgb(3, 47, 98);">'error'</span>, <span style="color: rgb(0, 92, 197);">title</span>: <span style="color: rgb(3, 47, 98);">'エラー'</span>, <span style="color: rgb(0, 92, 197);">text</span>: xhr.responseJSON?.message || <span style="color: rgb(3, 47, 98);">'不明なエラーが発生しました。'</span> }); } }); } }); </<span style="color: rgb(34, 134, 58);">script</span>> </code></pre><h2 style="border: 0px; border-collapse: collapse; border-spacing: 0px; list-style-position: initial; list-style-image: initial; list-style-type: revert; margin: revert; padding: revert; font: 22px / 24px roboto, helvetica, sans-serif; height: 32px; -webkit-line-clamp: 1; overflow: hidden; text-overflow: ellipsis; display: -webkit-box; -webkit-box-orient: vertical; color: rgba(0, 0, 0, 0.8); letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; white-space: normal; background-color: rgb(255, 255, 255); text-decoration-thickness: initial; text-decoration-style: initial; text-decoration-color: initial;"><strong style="font-weight: 700;">3. 改良ポイント</strong></h2><ul style="border: 0px; border-collapse: collapse; border-spacing: 0px; list-style-position: initial; list-style-image: initial; list-style-type: revert; margin: revert; padding: revert; color: rgba(0, 0, 0, 0.8); font-family: roboto, helvetica, sans-serif; font-size: 16px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; white-space: normal; background-color: rgb(255, 255, 255); text-decoration-thickness: initial; text-decoration-style: initial; text-decoration-color: initial;"><li style="border: 0px; border-collapse: collapse; border-spacing: 0px; list-style-position: initial; list-style-image: initial; list-style-type: revert; margin-top: revert; margin-right: revert; margin-bottom: 8px; margin-left: revert; padding: revert;"><strong style="font-weight: 700;">[ValidateAntiForgeryToken]</strong> と <code style="border-radius: 8px; background: none 0% 0% / auto repeat scroll padding-box border-box rgb(255, 255, 255);">RequestVerificationToken</code> ヘッダーで CSRF 対策を追加。</li><li style="border: 0px; border-collapse: collapse; border-spacing: 0px; list-style-position: initial; list-style-image: initial; list-style-type: revert; margin-top: revert; margin-right: revert; margin-bottom: 8px; margin-left: revert; padding: revert;">SweetAlert2 の <code style="border-radius: 8px; background: none 0% 0% / auto repeat scroll padding-box border-box rgb(255, 255, 255);">isConfirmed</code> を使って「Yes」クリック時のみ実行。</li><li style="border: 0px; border-collapse: collapse; border-spacing: 0px; list-style-position: initial; list-style-image: initial; list-style-type: revert; margin-top: revert; margin-right: revert; margin-bottom: 8px; margin-left: revert; padding: revert;">エラー時のメッセージを SweetAlert2 で表示。</li><li style="border: 0px; border-collapse: collapse; border-spacing: 0px; list-style-position: initial; list-style-image: initial; list-style-type: revert; margin-top: revert; margin-right: revert; margin-bottom: 8px; margin-left: revert; padding: revert;">成功後に <code style="border-radius: 8px; background: none 0% 0% / auto repeat scroll padding-box border-box rgb(255, 255, 255);">location.reload()</code> で画面更新(必要に応じて削除可)。</li><li style="border: 0px; border-collapse: collapse; border-spacing: 0px; list-style-position: initial; list-style-image: initial; list-style-type: revert; margin-top: revert; margin-right: revert; margin-bottom: 8px; margin-left: revert; padding: revert;">DTO を使って Controller 側で受け取るデータを明確化。</li></ul><p style="border: 0px; border-collapse: collapse; border-spacing: 0px; list-style-position: initial; list-style-image: initial; list-style-type: revert; margin: 4px; padding: revert; -webkit-line-clamp: revert; color: rgba(0, 0, 0, 0.8); font-family: roboto, helvetica, sans-serif; font-size: 16px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; white-space: normal; background-color: rgb(255, 255, 255); text-decoration-thickness: initial; text-decoration-style: initial; text-decoration-color: initial;">もしご希望であれば、このコードを <strong style="font-weight: 700;">Index.cshtml に完全統合した動作例</strong> としてまとめることも可能です。<br>そうすれば、View にそのまま貼り付けて動作確認できます。</p><p style="border: 0px; border-collapse: collapse; border-spacing: 0px; list-style-position: initial; list-style-image: initial; list-style-type: revert; margin: 4px; padding: revert; -webkit-line-clamp: revert; color: rgba(0, 0, 0, 0.8); font-family: roboto, helvetica, sans-serif; font-size: 16px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; white-space: normal; background-color: rgb(255, 255, 255); text-decoration-thickness: initial; text-decoration-style: initial; text-decoration-color: initial;">この統合版も作成しますか?<br>そうすると、あなたの環境ですぐ動く形になります。</p><p data-f-id="pbf" style="text-align: center; font-size: 14px; margin-top: 30px; opacity: 0.65; font-family: sans-serif;">Powered by <a href="https://www.froala.com/wysiwyg-editor?pb=1" title="Froala Editor">Froala Editor</a></p>
コピーする
添付画像
画像を変更
Back to List
//検索文:Editor内でHTMLコードをコピーさせる