asp.net mvc CRUD 添加/删除类别/产品
创始人
2024-09-16 12:00:10
0

下面是一个使用ASP.NET MVC实现CRUD操作的示例,包括添加/删除类别和产品的功能。

首先,创建一个Category模型类,用于表示类别:

public class Category
{
    public int Id { get; set; }
    public string Name { get; set; }
}

接下来,创建一个Product模型类,用于表示产品:

public class Product
{
    public int Id { get; set; }
    public string Name { get; set; }
    public decimal Price { get; set; }
    public int CategoryId { get; set; }
    public Category Category { get; set; }
}

然后,创建一个DbContext类,用于与数据库交互:

public class ApplicationDbContext : DbContext
{
    public DbSet Categories { get; set; }
    public DbSet Products { get; set; }

    protected override void OnModelCreating(ModelBuilder modelBuilder)
    {
        modelBuilder.Entity()
            .HasOne(p => p.Category)
            .WithMany(c => c.Products)
            .HasForeignKey(p => p.CategoryId);
    }
}

接下来,创建一个CategoryController,用于处理类别的CRUD操作:

public class CategoryController : Controller
{
    private readonly ApplicationDbContext _context;

    public CategoryController(ApplicationDbContext context)
    {
        _context = context;
    }

    public IActionResult Index()
    {
        var categories = _context.Categories.ToList();
        return View(categories);
    }

    public IActionResult Create()
    {
        return View();
    }

    [HttpPost]
    public IActionResult Create(Category category)
    {
        if (ModelState.IsValid)
        {
            _context.Categories.Add(category);
            _context.SaveChanges();
            return RedirectToAction("Index");
        }
        return View(category);
    }

    public IActionResult Delete(int id)
    {
        var category = _context.Categories.Find(id);
        if (category != null)
        {
            _context.Categories.Remove(category);
            _context.SaveChanges();
        }
        return RedirectToAction("Index");
    }
}

最后,创建一个ProductController,用于处理产品的CRUD操作:

public class ProductController : Controller
{
    private readonly ApplicationDbContext _context;

    public ProductController(ApplicationDbContext context)
    {
        _context = context;
    }

    public IActionResult Index()
    {
        var products = _context.Products.Include(p => p.Category).ToList();
        return View(products);
    }

    public IActionResult Create()
    {
        ViewBag.Categories = _context.Categories.ToList();
        return View();
    }

    [HttpPost]
    public IActionResult Create(Product product)
    {
        if (ModelState.IsValid)
        {
            _context.Products.Add(product);
            _context.SaveChanges();
            return RedirectToAction("Index");
        }
        ViewBag.Categories = _context.Categories.ToList();
        return View(product);
    }

    public IActionResult Delete(int id)
    {
        var product = _context.Products.Find(id);
        if (product != null)
        {
            _context.Products.Remove(product);
            _context.SaveChanges();
        }
        return RedirectToAction("Index");
    }
}

在以上示例中,我们使用Entity Framework Core来处理数据库操作。在视图层面,你可以创建相应的视图文件来呈现数据和用户界面。

请注意,以上示例仅提供了基本的CRUD操作,你可以根据自己的需求进行扩展和修改。

相关内容

热门资讯

查到实测!掌酷十三张怎么看有没... 查到实测!掌酷十三张怎么看有没有外挂(辅助)果然真的有挂(2022已更新)(哔哩哔哩);掌酷十三张怎...
wepoke辅助!wepoke... wepoke辅助!wepoke软件透明挂辅助(透视辅助)wePOke(好像真的有挂);一、wePOk...
微扑克德州专用辅助器!微扑克有... 微扑克德州专用辅助器!微扑克有挂,WPK好像是真的有挂,2025教程(有挂辅助挂)1、完成微扑克透视...
揭秘!牵手跑胡子辅助器最新版(... 揭秘!牵手跑胡子辅助器最新版(辅助挂)一贯是有挂(2022已更新)(哔哩哔哩);1、牵手跑胡子辅助器...
微扑克ai辅助器苹果版!微扑克... 您好,微扑克这款游戏可以开挂的,确实是有挂的,需要了解加微【136704302】很多玩家在这款游戏中...
wepoke辅助德之星!wep... wepoke辅助德之星!wepoke挂真的,wePoke果然真的有挂,德州教程(有挂教程);wePo...
分享实测!永和备厅有挂吗(辅助... 分享实测!永和备厅有挂吗(辅助)确实存在有挂(2026已更新)(哔哩哔哩)进入游戏-大厅左侧-新手福...
wepoke真的有挂!wepo... wepoke真的有挂!wepoke软件透明是真的(辅助透视)wepOkE(都是真的有挂);实战中需综...
微扑克辅助软件!微扑克有脚本,... 微扑克辅助软件!微扑克有脚本,德州微扑克辅助透视(切实有挂)1.微扑克 ai辅助创建新账号,点击进入...
一分钟了解!榕城510k外 挂... 一分钟了解!榕城510k外 挂(辅助挂)确实有挂(2020已更新)(哔哩哔哩);1、下载好榕城510...