ASP.NET MVCはじめました~データの削除
概要
CURD処理最後はデータの削除です。
環境
Visual Studio 2010
ASP.NET MVC2
SQLServer2008 R2
今回の要件は
- ブラウザより削除対象データを選択
- データベースより対象データを削除
として、サンプルコードを書いてみました。
実装
コントローラーの用意
using System; using System.Data; using System.Web.Mvc; using Mvc2App.Common; using Mvc2App.Framework.Search; using Mvc2App.Models; using Mvc2App.Services; using Mvc2App.Services.Abstractions; using Mvc2App.ViewModels.Peple; namespace Mvc2App.Controllers { [Authorize] public class PepleController : Controller { IPepleService _service; public PepleController() : this(new PepleService()) { } public PepleController(IPepleService service) { _service = service; } ... public ActionResult Delete(int id) { try { var model = _service.GetById(id); return View(model); } catch (ArgumentException) { return RedirectToAction("NotFound", "Home"); } } [HttpPost] public ActionResult Delete(PepleViewModel inputModel) { try { var person = new Person() { ID = inputModel.ID, Name = inputModel.Name, Address = inputModel.Address, PhoneNumber = inputModel.PhoneNumber, UpdatedBy = inputModel.UpdatedBy, UpdateDate = inputModel.UpdateDate }; _service.Delete(person); return RedirectToAction("Index"); } catch (UpdateException) { inputModel.SysMessage = Resource.GetValue("ERR_002"); return View(inputModel); } } } }
サービスインタフェースとサービスクラスの用意
using System; using System.Collections.Generic; using System.Web.Mvc; using Mvc2App.Framework.Search; using Mvc2App.Models; using Mvc2App.ViewModels; using Mvc2App.ViewModels.Peple; namespace Mvc2App.Services.Abstractions { public interface IPepleService { ... void Delete(Person p); } }
using System; using System.Collections.Generic; using System.Web.Mvc; using Mvc2App.Common; using Mvc2App.Framework.Search; using Mvc2App.Models; using Mvc2App.Repositories; using Mvc2App.Repositories.Abstractions; using Mvc2App.Services.Abstractions; using Mvc2App.ViewModels; using Mvc2App.ViewModels.Peple; namespace Mvc2App.Services { public class PepleService : IPepleService { private IPepleRepository _repository; public PepleService() : this(new PepleRepository()) { } public PepleService(IPepleRepository repository) { _repository = repository; } ... public void Delete(Person p) { _repository.Delete(p); } } }
リポジトリインタフェースとリポジトリクラスの用意
using System.Collections.Generic; using Mvc2App.Framework.Search; using Mvc2App.Models; namespace Mvc2App.Repositories.Abstractions { public interface IPepleRepository { ... void Delete(Person p); } }
using System; using System.Collections.Generic; using System.Linq; using Mvc2App.Framework.Search; using Mvc2App.Models; using Mvc2App.Repositories.Abstractions; namespace Mvc2App.Repositories { public class PepleRepository : IPepleRepository { DevEntities dbContext = new DevEntities(); ... public void Delete(Person p) { var peple = FindUpdateData(p.ID, p.UpdateDate); if (peple != null) { dbContext.Peple.DeleteObject(peple); dbContext.SaveChanges(); } } public Person FindUpdateData(int id, DateTime dt) { var person = dbContext.Peple.Where(m => m.ID == id && m.UpdateDate.Year == dt.Year && m.UpdateDate.Month == dt.Month && m.UpdateDate.Day == dt.Day && m.UpdateDate.Hour == dt.Hour && m.UpdateDate.Minute == dt.Minute && m.UpdateDate.Second == dt.Second && m.UpdateDate.Millisecond == dt.Millisecond ).FirstOrDefault(); return person; } } }
ビューの用意
<%@ Page Title="" Language="C#" MasterPageFile="~/Views/Shared/Site.Master" Inherits="System.Web.Mvc.ViewPage<Mvc2App.ViewModels.Peple.PepleViewModel>" %> <asp:Content ID="Content1" ContentPlaceHolderID="TitleContent" runat="server"> Delete </asp:Content> <asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="server"> <h2>Delete</h2> <h3>Are you sure you want to delete this?</h3> <table> <tr> <td> <div class="display-label">ID</div> </td> <td> <div class="display-field"><%: Model.ID %></div> </td> </tr> <tr> <td> <div class="display-label">Name</div> </td> <td> <div class="display-field"><%: Model.Name %></div> </td> </tr> <tr> <td> <div class="display-label">Address</div> </td> <td> <div class="display-field"><%: Model.Address %></div> </td> </tr> <tr> <td> <div class="display-label">PhoneNumber</div> </td> <td> <div class="display-field"><%: Model.PhoneNumber %></div> </td> </tr> <tr> <td> <div class="display-label">UpdatedBy</div> </td> <td> <div class="display-field"><%: Model.UpdatedBy %></div> </td> </tr> <tr> <td> <div class="display-label">UpdateDate</div> </td> <td> <div class="display-field"><%: Model.UpdateDate %></div> </td> </tr> </table> <% using (Html.BeginForm()) { %> <p> <%: Html.HiddenFor(model => model.Name)%> <%: Html.HiddenFor(model => model.Address)%> <%: Html.HiddenFor(model => model.PhoneNumber)%> <%: Html.HiddenFor(model => model.UpdatedBy)%> <%: Html.HiddenFor(model => model.UpdateDate) %> <input type="submit" value="Delete" /> | <%: Html.ActionLink("Back to List", "Index") %> </p> <% } %> </asp:Content>
実行結果
一覧画面より削除対象のデータを選択します。
ASP.NET MVCはじめました~データベースより値を取得し一覧表示する - 1.21 jigowatts
ASP.NET MVCはじめました~データベースより値を取得し詳細を表示する - 1.21 jigowatts
ASP.NET MVCはじめました~データの新規登録 - 1.21 jigowatts
ASP.NET MVCはじめました~データの更新と楽観的並行性制御 - 1.21 jigowatts