using CarInspection.Data;
using CarInspection.Models;
using CarInspection.Models.ViewModel;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
namespace CarInspection.Controllers
{
/// <summary>
/// 車両情報と車検情報をを一覧で表示する
/// </summary>
[Authorize]
public class CarAndInspectionController : Controller
{
private CarInspectionContext db = new CarInspectionContext();
// GET: CarAndInspection
public ActionResult Index()
{
return View();
}
[Authorize]
public ActionResult Details(int? id)
{
var loginUser = GetLoginUser();
if (id == null)
{
return new HttpStatusCodeResult(System.Net.HttpStatusCode.BadRequest);
}
var carAndInspection = new CarAndInspectionViewModel();
var car = loginUser.Cars.FirstOrDefault(c => c.Id == id);
if(car == null)
{
return new HttpStatusCodeResult(System.Net.HttpStatusCode.BadRequest);
}
carAndInspection.Car = car;
carAndInspection.inspectionLicenses = car.InspectionLicenses;
return View(carAndInspection);
}
private User GetLoginUser()
{
return db.Users.FirstOrDefault(u => u.UserName == User.Identity.Name);
}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
namespace CarInspection.Models.ViewModel
{
public class CarAndInspectionViewModel
{
public Car Car { get; set; }
public IEnumerable<InspectionLicense> inspectionLicenses { get; set; }
}
}
InspectionLicense
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 CarInspection.Data;
using CarInspection.Models;
using CarInspection.Models.ViewModel;
namespace CarInspection.Controllers
{
[Authorize]
public class InspectionLicensesController : Controller
{
private CarInspectionContext db = new CarInspectionContext();
// GET: InspectionLicenses/Details/5
public ActionResult Details(int? id,int? ParentCarId)
{
if (id == null || ParentCarId == null)
{
return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
}
InspectionLicense inspectionLicense = db.InspectionLicenses.Find(id);
if (inspectionLicense == null)
{
return HttpNotFound();
}
ViewBag.ParentCarId = ParentCarId;
return View(inspectionLicense);
}
// GET: InspectionLicenses/Create
public ActionResult Create(int? ParentCarId)
{
if(ParentCarId == null)
{
return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
}
ViewBag.ParentCarId = ParentCarId;
return View();
}
// POST: InspectionLicenses/Create
// 過多ポスティング攻撃を防止するには、バインド先とする特定のプロパティを有効にしてください。
// 詳細については、https://go.microsoft.com/fwlink/?LinkId=317598 を参照してください。
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Create([Bind(Include = "Id,Name,Memo,Expiration,CarId")] InspectionLicense inspectionLicense)
{
var carid = inspectionLicense.CarId;
var car = db.Cars.FirstOrDefault(c => c.Id == inspectionLicense.CarId);
if (ModelState.IsValid)
{
inspectionLicense.Created = DateTime.Now;
inspectionLicense.Updated = DateTime.Now;
inspectionLicense.Car = car;
db.InspectionLicenses.Add(inspectionLicense);
db.SaveChanges();
return RedirectToCarAndInspectionDetails(inspectionLicense.CarId);
}
return RedirectToCarAndInspectionDetails(inspectionLicense.CarId);
}
// GET: InspectionLicenses/Edit/5
public ActionResult Edit(int? id,int? ParentCarId)
{
if (id == null || ParentCarId == null)
{
return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
}
InspectionLicense inspectionLicense = db.InspectionLicenses.Find(id);
if (inspectionLicense == null)
{
return HttpNotFound();
}
ViewBag.ParentCarId = ParentCarId;
return View(inspectionLicense);
}
// POST: InspectionLicenses/Edit/5
// 過多ポスティング攻撃を防止するには、バインド先とする特定のプロパティを有効にしてください。
// 詳細については、https://go.microsoft.com/fwlink/?LinkId=317598 を参照してください。
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Edit([Bind(Include = "Id,Name,Memo,Expiration,CarId")] InspectionLicense inspectionLicense)
{
if (ModelState.IsValid)
{
inspectionLicense.Updated = DateTime.Now;
db.Entry(inspectionLicense).State = EntityState.Modified;
db.SaveChanges();
return RedirectToCarAndInspectionDetails(inspectionLicense.CarId);
}
return RedirectToCarAndInspectionDetails(inspectionLicense.CarId);
}
// GET: InspectionLicenses/Delete/5
public ActionResult Delete(int? id,int? ParentCarId)
{
if (id == null || ParentCarId == null)
{
return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
}
InspectionLicense inspectionLicense = db.InspectionLicenses.Find(id);
if (inspectionLicense == null)
{
return HttpNotFound();
}
ViewBag.ParentCarId = ParentCarId;
return View(inspectionLicense);
}
// POST: InspectionLicenses/Delete/5
[HttpPost, ActionName("Delete")]
[ValidateAntiForgeryToken]
public ActionResult DeleteConfirmed(int? id, int? ParentCarId)
{
if (ParentCarId == null)
{
return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
}
InspectionLicense inspectionLicense = db.InspectionLicenses.Find(id);
db.InspectionLicenses.Remove(inspectionLicense);
db.SaveChanges();
return RedirectToCarAndInspectionDetails(ParentCarId);
}
protected override void Dispose(bool disposing)
{
if (disposing)
{
db.Dispose();
}
base.Dispose(disposing);
}
private ActionResult RedirectToCarAndInspectionDetails(int? carId)
{
if(carId == null)
{
return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
}
else
{
return RedirectToAction("Details", "CarAndInspection",new { id = carId});
}
}
}
}
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
using System.Linq;
using System.Web;
using Microsoft.SqlServer.Server;
namespace CarInspection.Models
{
public class InspectionLicense
{
public int Id { get; set; }
[DisplayName("車検証名")]
public string Name { get; set; }
[DisplayName("備考")]
public string Memo { get; set; }
[DisplayName("満了日")]
[DataType(DataType.Date)]
public DateTime Expiration { get; set; }
public DateTime Created { get; set; }
public DateTime Updated { get; set; }
public virtual Car Car { get; set; }
[NotMapped]
public int CarId { get; set; }
}
HTML の name="UserName" が C# の Model プロパティ名と一致してないと、 Controller 側で値が null になる。 form 要素の name = Model property name が絶対則。 Razor の @Html.TextBoxFor(m => m.UserName) を使うと自動で揃うので推奨。
② [HttpPost] / [HttpGet] 指定漏れで GET POST 両方ヒット
Action メソッドに HTTP verb 属性を付け忘れると GET も POST もヒットする。 ブラウザ Back で意図せず Action 再実行 → 二重登録の事故。 form 送信用 Action は必ず [HttpPost]。
コメント