動くコード図鑑技術記事現場の渡り方キャリア論すべての記事About
C#

InspectionLicense

出典: ASP.NETでViewからControllerへ値を投げる方法!前提条件の確認! / InspectionLicense

InspectionLicense (csharp)#b21a612f1892
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});
        }
 
    }
}
▸ この snippet は実行結果未収録
▸ 実行結果は未収録です
  • id: #b21a612f1892
  • lines: 160
  • extracted: 2026-06-10

Source収録記事

この snippet は記事の「前提条件の確認! / InspectionLicense」セクションに登場する。コードの前後の文脈・ハマりどころの解説は記事本文で。

同じ記事から

6
図鑑トップ