如何解决如果未引用任何项目,则删除SQL
我正在图书馆管理网站上工作。我的数据库中有一个LibraryItem和Category表。
我想做的是,如果任何库项目中引用了任何类别,我希望用户不能删除它们。
因此,假设用库项目引用了名为“ DVD”的类别,并假设用户要删除“ DVD”类别。在这种情况下,我想告诉用户:
“无法删除该类别,因为它在一个项目中被引用,如果要删除DVD类别,请先删除该项目”
类别控制器如下:
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 LibraryProject;
namespace LibraryProject.Controllers
{
public class CategoriesController : Controller
{
private LibraryDbEntities db = new LibraryDbEntities();
// GET: Categories
public ActionResult Index()
{
return View(db.Category.ToList());
}
// GET: Categories/Details/5
public ActionResult Details(int? id)
{
if (id == null)
{
return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
}
Category category = db.Category.Find(id);
if (category == null)
{
return HttpNotFound();
}
return View(category);
}
// GET: Categories/Create
public ActionResult Create()
{
return View();
}
// POST: Categories/Create
// To protect from overposting attacks,enable the specific properties you want to bind to,for
// more details see https://go.microsoft.com/fwlink/?LinkId=317598.
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Create([Bind(Include = "Id,CategoryName")] Category category)
{
if (ModelState.IsValid)
{
// Check if the cat name exists or not
var cat = db.Category.FirstOrDefault(x => x.CategoryName == category.CategoryName);
if (cat == null)
{
db.Category.Add(category);
db.SaveChanges();
return RedirectToAction("Index");
}
else
{
// Add here the logic required to inform your user about the problem
ViewBag.ErrorMessage = "This Category name already exists,choose another one!";
}
}
return View(category);
}
// GET: Categories/Edit/5
public ActionResult Edit(int? id)
{
if (id == null)
{
return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
}
Category category = db.Category.Find(id);
if (category == null)
{
return HttpNotFound();
}
return View(category);
}
// POST: Categories/Edit/5
// To protect from overposting attacks,for
// more details see https://go.microsoft.com/fwlink/?LinkId=317598.
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Edit([Bind(Include = "Id,CategoryName")] Category category)
{
if (ModelState.IsValid)
{
db.Entry(category).State = EntityState.Modified;
db.SaveChanges();
return RedirectToAction("Index");
}
return View(category);
}
// GET: Categories/Delete/5
public ActionResult Delete(int? id)
{
if (id == null)
{
return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
}
Category category = db.Category.Find(id);
if (category == null)
{
return HttpNotFound();
}
return View(category);
}
// POST: Categories/Delete/5
[HttpPost,ActionName("Delete")]
[ValidateAntiForgeryToken]
public ActionResult DeleteConfirmed(int id)
{
Category category = db.Category.Find(id);
db.Category.Remove(category);
db.SaveChanges();
return RedirectToAction("Index");
}
protected override void dispose(bool disposing)
{
if (disposing)
{
db.dispose();
}
base.dispose(disposing);
}
}
}
我知道我必须在“删除”中编写一行代码,但是我只是不知道如何...所有帮助将不胜感激。
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。