mvc scaffold for ef

main
syneffort 2 years ago
parent e410809b9d
commit 9c7c661ce0
  1. 6
      AspNetFrameworkMVC/AspNetFrameworkMVC/AspNetFrameworkMVC.csproj
  2. 127
      AspNetFrameworkMVC/AspNetFrameworkMVC/Controllers/GuestsController.cs
  3. 56
      AspNetFrameworkMVC/AspNetFrameworkMVC/Views/Guests/Create.cshtml
  4. 48
      AspNetFrameworkMVC/AspNetFrameworkMVC/Views/Guests/Delete.cshtml
  5. 42
      AspNetFrameworkMVC/AspNetFrameworkMVC/Views/Guests/Details.cshtml
  6. 58
      AspNetFrameworkMVC/AspNetFrameworkMVC/Views/Guests/Edit.cshtml
  7. 45
      AspNetFrameworkMVC/AspNetFrameworkMVC/Views/Guests/Index.cshtml

@ -127,6 +127,7 @@
<Compile Include="App_Start\BundleConfig.cs" />
<Compile Include="App_Start\FilterConfig.cs" />
<Compile Include="App_Start\RouteConfig.cs" />
<Compile Include="Controllers\GuestsController.cs" />
<Compile Include="Controllers\HomeController.cs" />
<Compile Include="Global.asax.cs">
<DependentUpon>Global.asax</DependentUpon>
@ -195,6 +196,11 @@
<Content Include="Views\Home\GuestRazor.cshtml" />
<Content Include="Views\Home\ShowGuests.cshtml" />
<Content Include="Views\Home\Guest.cshtml" />
<Content Include="Views\Guests\Create.cshtml" />
<Content Include="Views\Guests\Delete.cshtml" />
<Content Include="Views\Guests\Details.cshtml" />
<Content Include="Views\Guests\Edit.cshtml" />
<Content Include="Views\Guests\Index.cshtml" />
</ItemGroup>
<ItemGroup />
<ItemGroup>

@ -0,0 +1,127 @@
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Linq;
using System.Net;
using System.Web;
using System.Web.Mvc;
using AspNetFrameworkMVC.Models;
namespace AspNetFrameworkMVC.Controllers
{
public class GuestsController : Controller
{
private GuestDbContext db = new GuestDbContext();
// GET: Guests
public ActionResult Index()
{
return View(db.Guests.ToList());
}
// GET: Guests/Details/5
public ActionResult Details(int? id)
{
if (id == null)
{
return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
}
Guest guest = db.Guests.Find(id);
if (guest == null)
{
return HttpNotFound();
}
return View(guest);
}
// GET: Guests/Create
public ActionResult Create()
{
return View();
}
// POST: Guests/Create
// 초과 게시 공격으로부터 보호하려면 바인딩하려는 특정 속성을 사용하도록 설정하세요.
// 자세한 내용은 https://go.microsoft.com/fwlink/?LinkId=317598을(를) 참조하세요.
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Create([Bind(Include = "Id,Name,CreatedDate,Message")] Guest guest)
{
if (ModelState.IsValid)
{
db.Guests.Add(guest);
db.SaveChanges();
return RedirectToAction("Index");
}
return View(guest);
}
// GET: Guests/Edit/5
public ActionResult Edit(int? id)
{
if (id == null)
{
return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
}
Guest guest = db.Guests.Find(id);
if (guest == null)
{
return HttpNotFound();
}
return View(guest);
}
// POST: Guests/Edit/5
// 초과 게시 공격으로부터 보호하려면 바인딩하려는 특정 속성을 사용하도록 설정하세요.
// 자세한 내용은 https://go.microsoft.com/fwlink/?LinkId=317598을(를) 참조하세요.
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Edit([Bind(Include = "Id,Name,CreatedDate,Message")] Guest guest)
{
if (ModelState.IsValid)
{
db.Entry(guest).State = EntityState.Modified;
db.SaveChanges();
return RedirectToAction("Index");
}
return View(guest);
}
// GET: Guests/Delete/5
public ActionResult Delete(int? id)
{
if (id == null)
{
return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
}
Guest guest = db.Guests.Find(id);
if (guest == null)
{
return HttpNotFound();
}
return View(guest);
}
// POST: Guests/Delete/5
[HttpPost, ActionName("Delete")]
[ValidateAntiForgeryToken]
public ActionResult DeleteConfirmed(int id)
{
Guest guest = db.Guests.Find(id);
db.Guests.Remove(guest);
db.SaveChanges();
return RedirectToAction("Index");
}
protected override void Dispose(bool disposing)
{
if (disposing)
{
db.Dispose();
}
base.Dispose(disposing);
}
}
}

@ -0,0 +1,56 @@
@model AspNetFrameworkMVC.Models.Guest
@{
ViewBag.Title = "Create";
}
<h2>Create</h2>
@using (Html.BeginForm())
{
@Html.AntiForgeryToken()
<div class="form-horizontal">
<h4>Guest</h4>
<hr />
@Html.ValidationSummary(true, "", new { @class = "text-danger" })
<div class="form-group">
@Html.LabelFor(model => model.Name, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.Name, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.Name, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.CreatedDate, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.CreatedDate, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.CreatedDate, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.Message, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.Message, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.Message, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
<div class="col-md-offset-2 col-md-10">
<input type="submit" value="Create" class="btn btn-default" />
</div>
</div>
</div>
}
<div>
@Html.ActionLink("Back to List", "Index")
</div>
@section Scripts {
@Scripts.Render("~/bundles/jqueryval")
}

@ -0,0 +1,48 @@
@model AspNetFrameworkMVC.Models.Guest
@{
ViewBag.Title = "Delete";
}
<h2>Delete</h2>
<h3>Are you sure you want to delete this?</h3>
<div>
<h4>Guest</h4>
<hr />
<dl class="dl-horizontal">
<dt>
@Html.DisplayNameFor(model => model.Name)
</dt>
<dd>
@Html.DisplayFor(model => model.Name)
</dd>
<dt>
@Html.DisplayNameFor(model => model.CreatedDate)
</dt>
<dd>
@Html.DisplayFor(model => model.CreatedDate)
</dd>
<dt>
@Html.DisplayNameFor(model => model.Message)
</dt>
<dd>
@Html.DisplayFor(model => model.Message)
</dd>
</dl>
@using (Html.BeginForm()) {
@Html.AntiForgeryToken()
<div class="form-actions no-color">
<input type="submit" value="Delete" class="btn btn-default" /> |
@Html.ActionLink("Back to List", "Index")
</div>
}
</div>

@ -0,0 +1,42 @@
@model AspNetFrameworkMVC.Models.Guest
@{
ViewBag.Title = "Details";
}
<h2>Details</h2>
<div>
<h4>Guest</h4>
<hr />
<dl class="dl-horizontal">
<dt>
@Html.DisplayNameFor(model => model.Name)
</dt>
<dd>
@Html.DisplayFor(model => model.Name)
</dd>
<dt>
@Html.DisplayNameFor(model => model.CreatedDate)
</dt>
<dd>
@Html.DisplayFor(model => model.CreatedDate)
</dd>
<dt>
@Html.DisplayNameFor(model => model.Message)
</dt>
<dd>
@Html.DisplayFor(model => model.Message)
</dd>
</dl>
</div>
<p>
@Html.ActionLink("Edit", "Edit", new { id = Model.Id }) |
@Html.ActionLink("Back to List", "Index")
</p>

@ -0,0 +1,58 @@
@model AspNetFrameworkMVC.Models.Guest
@{
ViewBag.Title = "Edit";
}
<h2>Edit</h2>
@using (Html.BeginForm())
{
@Html.AntiForgeryToken()
<div class="form-horizontal">
<h4>Guest</h4>
<hr />
@Html.ValidationSummary(true, "", new { @class = "text-danger" })
@Html.HiddenFor(model => model.Id)
<div class="form-group">
@Html.LabelFor(model => model.Name, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.Name, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.Name, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.CreatedDate, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.CreatedDate, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.CreatedDate, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.Message, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.Message, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.Message, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
<div class="col-md-offset-2 col-md-10">
<input type="submit" value="Save" class="btn btn-default" />
</div>
</div>
</div>
}
<div>
@Html.ActionLink("Back to List", "Index")
</div>
@section Scripts {
@Scripts.Render("~/bundles/jqueryval")
}

@ -0,0 +1,45 @@
@model IEnumerable<AspNetFrameworkMVC.Models.Guest>
@{
ViewBag.Title = "Index";
}
<h2>Index</h2>
<p>
@Html.ActionLink("Create New", "Create")
</p>
<table class="table">
<tr>
<th>
@Html.DisplayNameFor(model => model.Name)
</th>
<th>
@Html.DisplayNameFor(model => model.CreatedDate)
</th>
<th>
@Html.DisplayNameFor(model => model.Message)
</th>
<th></th>
</tr>
@foreach (var item in Model) {
<tr>
<td>
@Html.DisplayFor(modelItem => item.Name)
</td>
<td>
@Html.DisplayFor(modelItem => item.CreatedDate)
</td>
<td>
@Html.DisplayFor(modelItem => item.Message)
</td>
<td>
@Html.ActionLink("Edit", "Edit", new { id=item.Id }) |
@Html.ActionLink("Details", "Details", new { id=item.Id }) |
@Html.ActionLink("Delete", "Delete", new { id=item.Id })
</td>
</tr>
}
</table>
Loading…
Cancel
Save