SkyoceanHome
Home
Privacy
ダウンロード
SkyShop
Updated Information
チュートリアルコード集
References
記事編集
日付
ジャンルのカテゴリ―名
名前を選択して下さい。
Contexts
Controllers
Databases
Htmls
JavaScripts
Models
MVCTutorials
Others
Styles
Views
タイトル
内容の要旨
<p data-bm="65" 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">ASP.NET MVC で「1~12 の整数を入力し、データベースに重複しないように保存する」Controller 側のサンプルコードを示します。<br>Entity Framework(EF Core または EF6)を使った例です。</p><h2 data-bm="66" 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;">Controller サンプルコード</h2><pre data-priority="2" data-bm="67" style="background: none 0% 0% / auto repeat scroll padding-box border-box rgb(240, 245, 245); border-color: rgba(0, 0, 0, 0.1); border-style: solid; border-width: 1px; border-image: none 100% / 1 / 0 stretch; 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="74" 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(242, 244, 245);"><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(60, 81, 180);">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(217, 223, 251); border-color: rgba(0, 0, 0, 0); border-style: solid; border-width: 1px; border-image: none 100% / 1 / 0 stretch; display: grid; grid-auto-flow: column; align-items: center; cursor: pointer; color: rgb(60, 81, 180) !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> System; <span style="color: rgb(215, 58, 73);">using</span> System.Linq; <span style="color: rgb(215, 58, 73);">using</span> System.Web.Mvc; <span style="color: rgb(106, 115, 125);">// ASP.NET MVC 5 の場合</span> <span style="color: rgb(215, 58, 73);">using</span> YourApp.Models; <span style="color: rgb(106, 115, 125);">// DbContext と Model の名前空間</span> <span style="color: rgb(215, 58, 73);">namespace</span> <span style="color: rgb(111, 66, 193);">YourApp.Controllers</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);">MonthController</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 _db = <span style="color: rgb(215, 58, 73);">new</span> ApplicationDbContext(); <span style="color: rgb(106, 115, 125);">// GET: 入力フォーム表示</span> <span style="color: rgb(215, 58, 73);">public</span> ActionResult <span style="color: rgb(111, 66, 193);">Create</span>() { <span style="color: rgb(215, 58, 73);">return</span> View(); } <span style="color: rgb(106, 115, 125);">// POST: データ登録</span> [<span style="color: rgb(0, 92, 197);">HttpPost</span>] [<span style="color: rgb(0, 92, 197);">ValidateAntiForgeryToken</span>] <span style="color: rgb(215, 58, 73);">public</span> ActionResult <span style="color: rgb(111, 66, 193);">Create</span>(MonthEntry model) { <span style="color: rgb(215, 58, 73);">try</span> { <span style="color: rgb(106, 115, 125);">// 入力値のバリデーション(1~12 の範囲)</span> <span style="color: rgb(215, 58, 73);">if</span> (model.Value < <span style="color: rgb(0, 92, 197);">1</span> || model.Value > <span style="color: rgb(0, 92, 197);">12</span>) { ModelState.AddModelError(<span style="color: rgb(3, 47, 98);">"Value"</span>, <span style="color: rgb(3, 47, 98);">"1~12 の整数を入力してください。"</span>); } <span style="color: rgb(106, 115, 125);">// 重複チェック</span> <span style="color: rgb(227, 98, 9);">bool</span> exists = _db.MonthEntries.Any(m => m.Value == model.Value); <span style="color: rgb(215, 58, 73);">if</span> (exists) { ModelState.AddModelError(<span style="color: rgb(3, 47, 98);">"Value"</span>, <span style="color: rgb(3, 47, 98);">"この値は既に登録されています。"</span>); } <span style="color: rgb(106, 115, 125);">// バリデーションエラーがあればフォームに戻す</span> <span style="color: rgb(215, 58, 73);">if</span> (!ModelState.IsValid) { <span style="color: rgb(215, 58, 73);">return</span> View(model); } <span style="color: rgb(106, 115, 125);">// 登録処理</span> _db.MonthEntries.Add(model); _db.SaveChanges(); TempData[<span style="color: rgb(3, 47, 98);">"Message"</span>] = <span style="color: rgb(3, 47, 98);">"登録が完了しました。"</span>; <span style="color: rgb(215, 58, 73);">return</span> RedirectToAction(<span style="color: rgb(3, 47, 98);">"Index"</span>); } <span style="color: rgb(215, 58, 73);">catch</span> (Exception ex) { <span style="color: rgb(106, 115, 125);">// ログ出力など(省略)</span> ModelState.AddModelError(<span style="color: rgb(3, 47, 98);">""</span>, <span style="color: rgb(3, 47, 98);">"エラーが発生しました: "</span> + ex.Message); <span style="color: rgb(215, 58, 73);">return</span> View(model); } } <span style="color: rgb(106, 115, 125);">// 一覧表示</span> <span style="color: rgb(215, 58, 73);">public</span> ActionResult <span style="color: rgb(111, 66, 193);">Index</span>() { <span style="color: rgb(215, 58, 73);">var</span> list = _db.MonthEntries.OrderBy(m => m.Value).ToList(); <span style="color: rgb(215, 58, 73);">return</span> View(list); } <span style="color: rgb(215, 58, 73);">protected</span> <span style="color: rgb(215, 58, 73);">override</span> <span style="color: rgb(215, 58, 73);">void</span> <span style="color: rgb(111, 66, 193);">Dispose</span>(<span style="color: rgb(227, 98, 9);">bool</span> disposing) { <span style="color: rgb(215, 58, 73);">if</span> (disposing) { _db.Dispose(); } <span style="color: rgb(215, 58, 73);">base</span>.Dispose(disposing); } } } </code></pre><h2 data-bm="68" 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;">モデル例</h2><pre data-priority="2" data-bm="69" style="background: none 0% 0% / auto repeat scroll padding-box border-box rgb(240, 245, 245); border-color: rgba(0, 0, 0, 0.1); border-style: solid; border-width: 1px; border-image: none 100% / 1 / 0 stretch; 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="75" 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(242, 244, 245);"><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(60, 81, 180);">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(217, 223, 251); border-color: rgba(0, 0, 0, 0); border-style: solid; border-width: 1px; border-image: none 100% / 1 / 0 stretch; display: grid; grid-auto-flow: column; align-items: center; cursor: pointer; color: rgb(60, 81, 180) !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> System.ComponentModel.DataAnnotations; <span style="color: rgb(215, 58, 73);">namespace</span> <span style="color: rgb(111, 66, 193);">YourApp.Models</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);">MonthEntry</span> { <span style="color: rgb(215, 58, 73);">public</span> <span style="color: rgb(227, 98, 9);">int</span> Id { <span style="color: rgb(215, 58, 73);">get</span>; <span style="color: rgb(215, 58, 73);">set</span>; } [<span style="color: rgb(0, 92, 197);">Required</span>] [<span style="color: rgb(0, 92, 197);">Range(1, 12, ErrorMessage = <span style="color: rgb(3, 47, 98);">"1~12 の整数を入力してください。"</span>)</span>] <span style="color: rgb(215, 58, 73);">public</span> <span style="color: rgb(227, 98, 9);">int</span> Value { <span style="color: rgb(215, 58, 73);">get</span>; <span style="color: rgb(215, 58, 73);">set</span>; } } } </code></pre><h2 data-bm="70" 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;">ポイント</h2><ol data-bm="71" 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;">範囲チェック</strong><br><code style="border-radius: 8px; background: none 0% 0% / auto repeat scroll padding-box border-box rgb(255, 255, 255);">[Range(1, 12)]</code> 属性でモデルレベルの検証を行い、Controller 側でも念のためチェック。</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;"><strong style="font-weight: 700;">重複チェック</strong><br><code style="border-radius: 8px; background: none 0% 0% / auto repeat scroll padding-box border-box rgb(255, 255, 255);">Any()</code> を使って DB に同じ値が存在するか確認。</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;"><strong style="font-weight: 700;">ModelState</strong><br>エラーがあれば <code style="border-radius: 8px; background: none 0% 0% / auto repeat scroll padding-box border-box rgb(255, 255, 255);">ModelState.AddModelError()</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;"><strong style="font-weight: 700;">例外処理</strong><br>DB 保存時のエラーもキャッチして安全に処理。</li></ol><p data-bm="72" 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;">Controller 側で重複を防ぐ</strong> 方法ですが、<br>さらに <strong style="font-weight: 700;">DB 側でユニーク制約(Unique Index)</strong> を設定しておくと、同時アクセス時の競合も防げます。</p><p data-bm="73" 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;">ユニーク制約付きのマイグレーション</strong> を追加した完全版も作れますが、作成しますか?<br>そうすれば Controller 側と DB 側の両方で二重防止できます。</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コードをコピーさせる