Skip to content

Commit 5177a9c

Browse files
USER001 FUSER001 F
authored andcommitted
Add _ValidationScriptsPartial.cshtml
重要、スクリプト関係
1 parent 3515075 commit 5177a9c

File tree

10 files changed

+341
-22
lines changed

10 files changed

+341
-22
lines changed
Lines changed: 150 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,150 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
using System.Threading.Tasks;
5+
using Microsoft.AspNetCore.Mvc;
6+
using Microsoft.AspNetCore.Mvc.Rendering;
7+
using Microsoft.EntityFrameworkCore;
8+
using WebApplication.Models;
9+
10+
namespace WebApplication.Controllers
11+
{
12+
public class ValuesController : Controller
13+
{
14+
private readonly SQLiteDbContext _context;
15+
16+
public ValuesController(SQLiteDbContext context)
17+
{
18+
_context = context;
19+
}
20+
21+
// GET: Values
22+
public async Task<IActionResult> Index()
23+
{
24+
return View(await _context.Values.ToListAsync());
25+
}
26+
27+
// GET: Values/Details/5
28+
public async Task<IActionResult> Details(int? id)
29+
{
30+
if (id == null)
31+
{
32+
return NotFound();
33+
}
34+
35+
var value = await _context.Values.SingleOrDefaultAsync(m => m.Id == id);
36+
if (value == null)
37+
{
38+
return NotFound();
39+
}
40+
41+
return View(value);
42+
}
43+
44+
// GET: Values/Create
45+
public IActionResult Create()
46+
{
47+
return View();
48+
}
49+
50+
// POST: Values/Create
51+
// To protect from overposting attacks, please enable the specific properties you want to bind to, for
52+
// more details see http://go.microsoft.com/fwlink/?LinkId=317598.
53+
[HttpPost]
54+
[ValidateAntiForgeryToken]
55+
public async Task<IActionResult> Create([Bind("Id,Name")] Value value)
56+
{
57+
if (ModelState.IsValid)
58+
{
59+
_context.Add(value);
60+
await _context.SaveChangesAsync();
61+
return RedirectToAction("Index");
62+
}
63+
return View(value);
64+
}
65+
66+
// GET: Values/Edit/5
67+
public async Task<IActionResult> Edit(int? id)
68+
{
69+
if (id == null)
70+
{
71+
return NotFound();
72+
}
73+
74+
var value = await _context.Values.SingleOrDefaultAsync(m => m.Id == id);
75+
if (value == null)
76+
{
77+
return NotFound();
78+
}
79+
return View(value);
80+
}
81+
82+
// POST: Values/Edit/5
83+
// To protect from overposting attacks, please enable the specific properties you want to bind to, for
84+
// more details see http://go.microsoft.com/fwlink/?LinkId=317598.
85+
[HttpPost]
86+
[ValidateAntiForgeryToken]
87+
public async Task<IActionResult> Edit(int id, [Bind("Id,Name")] Value value)
88+
{
89+
if (id != value.Id)
90+
{
91+
return NotFound();
92+
}
93+
94+
if (ModelState.IsValid)
95+
{
96+
try
97+
{
98+
_context.Update(value);
99+
await _context.SaveChangesAsync();
100+
}
101+
catch (DbUpdateConcurrencyException)
102+
{
103+
if (!ValueExists(value.Id))
104+
{
105+
return NotFound();
106+
}
107+
else
108+
{
109+
throw;
110+
}
111+
}
112+
return RedirectToAction("Index");
113+
}
114+
return View(value);
115+
}
116+
117+
// GET: Values/Delete/5
118+
public async Task<IActionResult> Delete(int? id)
119+
{
120+
if (id == null)
121+
{
122+
return NotFound();
123+
}
124+
125+
var value = await _context.Values.SingleOrDefaultAsync(m => m.Id == id);
126+
if (value == null)
127+
{
128+
return NotFound();
129+
}
130+
131+
return View(value);
132+
}
133+
134+
// POST: Values/Delete/5
135+
[HttpPost, ActionName("Delete")]
136+
[ValidateAntiForgeryToken]
137+
public async Task<IActionResult> DeleteConfirmed(int id)
138+
{
139+
var value = await _context.Values.SingleOrDefaultAsync(m => m.Id == id);
140+
_context.Values.Remove(value);
141+
await _context.SaveChangesAsync();
142+
return RedirectToAction("Index");
143+
}
144+
145+
private bool ValueExists(int id)
146+
{
147+
return _context.Values.Any(e => e.Id == id);
148+
}
149+
}
150+
}

WebAppScaffoldSample/src/WebApplication/Models/SQLiteDbContext.cs

Lines changed: 0 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -17,21 +17,6 @@ public SQLiteDbContext(DbContextOptions<SQLiteDbContext> options) :base(options)
1717

1818
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
1919
{
20-
if (!optionsBuilder.IsConfigured)
21-
{
22-
var config = new ConfigurationBuilder()
23-
.SetBasePath(Directory.GetCurrentDirectory())
24-
.AddJsonFile("appsettings.json")
25-
.Build();
26-
27-
var dbkind = config["Data:DefaultConnection:ConnectionDBString"];
28-
if(dbkind.Equals("sqlite"))
29-
{
30-
optionsBuilder.UseSqlite(config["Data:DefaultConnection:ConnectionString"]);
31-
}
32-
}
33-
34-
base.OnConfiguring(optionsBuilder);
3520
}
3621

3722
public DbSet<Value> Values { get; set; }

WebAppScaffoldSample/src/WebApplication/Views/Shared/_Layout.cshtml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@
3333
<li><a asp-area="" asp-controller="Home" asp-action="Index">Home</a></li>
3434
<li><a asp-area="" asp-controller="Home" asp-action="About">About</a></li>
3535
<li><a asp-area="" asp-controller="Home" asp-action="Contact">Contact</a></li>
36+
<li><a asp-area="" asp-controller="Values" asp-action="">Values</a></li>
3637
</ul>
3738
</div>
3839
</div>
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
<environment names="Development">
2+
<script src="~/lib/jquery-validation/dist/jquery.validate.js"></script>
3+
<script src="~/lib/jquery-validation-unobtrusive/jquery.validate.unobtrusive.js"></script>
4+
</environment>
5+
<environment names="Staging,Production">
6+
<script src="https://ajax.aspnetcdn.com/ajax/jquery.validate/1.15.0/jquery.validate.min.js"
7+
asp-fallback-src="~/lib/jquery-validation/dist/jquery.validate.min.js"
8+
asp-fallback-test="window.jQuery && window.jQuery.validator">
9+
</script>
10+
<script src="https://ajax.aspnetcdn.com/ajax/jquery.validation.unobtrusive/3.2.6/jquery.validate.unobtrusive.min.js"
11+
asp-fallback-src="~/lib/jquery-validation-unobtrusive/jquery.validate.unobtrusive.min.js"
12+
asp-fallback-test="window.jQuery && window.jQuery.validator && window.jQuery.validator.unobtrusive">
13+
</script>
14+
</environment>
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
@model WebApplication.Models.Value
2+
3+
@{
4+
ViewData["Title"] = "Create";
5+
}
6+
7+
<h2>Create</h2>
8+
9+
<form asp-action="Create">
10+
<div class="form-horizontal">
11+
<h4>Value</h4>
12+
<hr />
13+
<div asp-validation-summary="ModelOnly" class="text-danger"></div>
14+
<div class="form-group">
15+
<label asp-for="Name" class="col-md-2 control-label"></label>
16+
<div class="col-md-10">
17+
<input asp-for="Name" class="form-control" />
18+
<span asp-validation-for="Name" class="text-danger" />
19+
</div>
20+
</div>
21+
<div class="form-group">
22+
<div class="col-md-offset-2 col-md-10">
23+
<input type="submit" value="Create" class="btn btn-default" />
24+
</div>
25+
</div>
26+
</div>
27+
</form>
28+
29+
<div>
30+
<a asp-action="Index">Back to List</a>
31+
</div>
32+
33+
@section Scripts {
34+
@{await Html.RenderPartialAsync("_ValidationScriptsPartial");}
35+
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
@model WebApplication.Models.Value
2+
3+
@{
4+
ViewData["Title"] = "Delete";
5+
}
6+
7+
<h2>Delete</h2>
8+
9+
<h3>Are you sure you want to delete this?</h3>
10+
<div>
11+
<h4>Value</h4>
12+
<hr />
13+
<dl class="dl-horizontal">
14+
<dt>
15+
@Html.DisplayNameFor(model => model.Name)
16+
</dt>
17+
<dd>
18+
@Html.DisplayFor(model => model.Name)
19+
</dd>
20+
</dl>
21+
22+
<form asp-action="Delete">
23+
<div class="form-actions no-color">
24+
<input type="submit" value="Delete" class="btn btn-default" /> |
25+
<a asp-action="Index">Back to List</a>
26+
</div>
27+
</form>
28+
</div>
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
@model WebApplication.Models.Value
2+
3+
@{
4+
ViewData["Title"] = "Details";
5+
}
6+
7+
<h2>Details</h2>
8+
9+
<div>
10+
<h4>Value</h4>
11+
<hr />
12+
<dl class="dl-horizontal">
13+
<dt>
14+
@Html.DisplayNameFor(model => model.Name)
15+
</dt>
16+
<dd>
17+
@Html.DisplayFor(model => model.Name)
18+
</dd>
19+
</dl>
20+
</div>
21+
<div>
22+
<a asp-action="Edit" asp-route-id="@Model.Id">Edit</a> |
23+
<a asp-action="Index">Back to List</a>
24+
</div>
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
@model WebApplication.Models.Value
2+
3+
@{
4+
ViewData["Title"] = "Edit";
5+
}
6+
7+
<h2>Edit</h2>
8+
9+
<form asp-action="Edit">
10+
<div class="form-horizontal">
11+
<h4>Value</h4>
12+
<hr />
13+
<div asp-validation-summary="ModelOnly" class="text-danger"></div>
14+
<input type="hidden" asp-for="Id" />
15+
<div class="form-group">
16+
<label asp-for="Name" class="col-md-2 control-label"></label>
17+
<div class="col-md-10">
18+
<input asp-for="Name" class="form-control" />
19+
<span asp-validation-for="Name" class="text-danger" />
20+
</div>
21+
</div>
22+
<div class="form-group">
23+
<div class="col-md-offset-2 col-md-10">
24+
<input type="submit" value="Save" class="btn btn-default" />
25+
</div>
26+
</div>
27+
</div>
28+
</form>
29+
30+
<div>
31+
<a asp-action="Index">Back to List</a>
32+
</div>
33+
34+
@section Scripts {
35+
@{await Html.RenderPartialAsync("_ValidationScriptsPartial");}
36+
}
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
@model IEnumerable<WebApplication.Models.Value>
2+
3+
@{
4+
ViewData["Title"] = "Index";
5+
}
6+
7+
<h2>Index</h2>
8+
9+
<p>
10+
<a asp-action="Create">Create New</a>
11+
</p>
12+
<table class="table">
13+
<thead>
14+
<tr>
15+
<th>
16+
@Html.DisplayNameFor(model => model.Name)
17+
</th>
18+
<th></th>
19+
</tr>
20+
</thead>
21+
<tbody>
22+
@foreach (var item in Model) {
23+
<tr>
24+
<td>
25+
@Html.DisplayFor(modelItem => item.Name)
26+
</td>
27+
<td>
28+
<a asp-action="Edit" asp-route-id="@item.Id">Edit</a> |
29+
<a asp-action="Details" asp-route-id="@item.Id">Details</a> |
30+
<a asp-action="Delete" asp-route-id="@item.Id">Delete</a>
31+
</td>
32+
</tr>
33+
}
34+
</tbody>
35+
</table>

0 commit comments

Comments
 (0)